获取选定文件的POSIX路径,包括网络驱动器上的文件的AppleScript

3
我正在尝试实现一项功能,即在OSX Finder中右键单击视频文件,按下“服务”和我的脚本名称,然后MediaInfo会在终端中加载并显示视频标签数据。我已经成功实现了对本地驱动器中文件的操作。但是对于网络驱动器,由于路径中缺少/Volumes,因此MediaInfo找不到该文件。
我已将以下脚本放置在Automator的“运行AppleScript”中,并保存为服务(Automator顶部的“服务接收Finder中选择的文件或文件夹”):
tell application "Finder"
    set theItems to selection
    repeat with itemRef in theItems
        set myfolder to (get folder of itemRef) as string
        set myfolder to POSIX path of myfolder
        set myfile to "'" & myfolder & (get name of itemRef) & "'"
    end repeat -- it will store the last filename in selection
end tell

tell application "Terminal"
    activate
    do script

    set size of window 1 to {1200, 1200}

    set cmd to "mediainfo " & myfile
    try
        if busy of selected tab of window 1 then error
        do script with command cmd in window 1
    on error
        do script with command cmd
    end try
end tell

那么,如果涉及网络驱动器,要如何更改路径以在开头包含 /Volumes 呢?

3个回答

1
有几个事情需要注意,不太清楚为什么要分离文件名然后重新添加它。有时候使用项目的名称可能会包括扩展名,但有时候不包括。这在网络驱动器上尤其如此。 另外,不要把不需要放在 Finder 块中的东西放进去。对我来说,上半部分在选定的文件和文件夹上运作良好,包括网络驱动器和 posix 路径中的卷。
tell application "Finder" to set theItems to selection
repeat with itemRef in theItems
    set myitem to POSIX path of (itemRef as string)
end repeat -- it will store the last filename in selection

谢谢,不过那只给了路径。所以这似乎可以解决问题:告诉应用程序 "Finder" 选择项目;重复处理 theItems;使用项目参照中的 set 将 myitem 设置为“'”和 (itemRef as string) 的 POSIX 路径;end repeat - 但是我在终端部分也遇到了很大的问题。无论我尝试什么,它都会打开两个窗口或执行一些不必要的操作。如何才能简单地为我的命令始终打开一个新终端(而不是双倍并且不是已经打开的终端)? - Jim Hoyle

1

经过一些试错,这似乎是一个适用于我的目的的工作脚本(将此脚本作为上下文菜单项运行,在一个放大的新终端窗口中显示MediaInfo输出):

tell application "Finder" to set theItems to selection
repeat with itemRef in theItems
    set myitem to "'" & POSIX path of (itemRef as string) & "'"
end repeat -- it will store the last filename in selection

tell application "Terminal"
    launch
    activate
    do script
    set size of window 1 to {1200, 1200}
    do script "mediainfo " & myitem in window 1
end tell

当有多个带空格的参数时,行“set myitem to”由于引号而无法工作。使用""而不是' '似乎可以解决这个问题。 - Grzegorz Adam Hankiewicz

0

您可以获取Finder项目的Disk属性,然后检查磁盘是否具有设置为false的本地卷属性。

我没有尝试过这个,但我认为您将不得不将该项作为一个项来处理。(set theDisk to disk of item yourAlias)。

至少在理论上,这应该会让您更接近一点,我认为要使其工作,您需要知道posix路径应该是什么样子,例如/Volumes/the network volume/the Path to/file等等。

Finder还具有启动磁盘属性,如果这对您更好的话,我认为您可以使用它来比较驱动器。

所有内容都在AppleScript编辑器中的Finder字典中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接