如何使用AppleScript或shell脚本在OSX中获取文件URL?

时间:2012-03-08 11:36:15

标签: macos shell url applescript finder

我尝试一起使用Keyboard Maestro快捷方式来为我提供文件的URL(就像Path Finder输出的那样)。

到目前为止,我使用以下AppleScript并将空格替换为%20

tell application "Finder"
    set sel to the selection as text
    set the clipboard to POSIX path of sel
end tell

然后我只是追加file://localhost/

问题出在特殊字符上,例如我的桌面上有以下文件夹:

我的输出:file://localhost//Users/patte/Desktop/#%20Old%20files

正确的输出应该转换哈希:file://localhost/Users/patte/Desktop/%23%20Old%20files


使用AppleScript或Shell脚本的解决方案会很棒,因为我能够合并它。我也试过set the clipboard to URL of the first item of (get the selection),但这对我没用 - 也许我做错了。

另一种选择是编写特殊字符的脚本 - 我也可以使用它,但我不确定要转换成什么 - 否则我会寻找它。

5 个答案:

答案 0 :(得分:4)

这几乎逐字逐句地从this answer解除,即在将file://localhost添加到字符串的开头之前,利用python的urllib正确引用字符串

on path2url(thepath)
    return do shell script "python -c \"import urllib, sys; print (urllib.quote(sys.argv[1]))\" " & quoted form of thepath
end path2url

tell application "Finder"
    set sel to the selection as text
    set the clipboard to "file://localhost" & my path2url(POSIX path of sel)
end tell

我在打印周围添加了括号,以使python脚本在python 2.x和python 3之间兼容。

答案 1 :(得分:4)

这是一个简单的AppleScript,它将遍历Finder选择并将文件URL放在剪贴板上,并以返回分隔的字符串形式显示。它使用mklement的“2行”代码,可与Keyboard Maestro一起使用:

set theOutput to ""

-- Obtain Finder selection and store it in variable "sel".
tell application "Finder" to set sel to get selection as alias list

repeat with x in sel

-- Convert to alias, then determine its file URL and store that in variable "myFileUrl"
tell application "System Events" to set myFileUrl to URL of x

if theOutput = "" then
    set theOutput to myFileUrl
else
    set theOutput to theOutput & return & myFileUrl
end if

end repeat

set the clipboard to theOutput

答案 2 :(得分:2)

有一个更强大和方便的解决方案(在10.7.4上测试 - 不知道何时可用):

-- Obtain Finder selection and store it in variable "sel".
set sel to selection of application "Finder"
-- Convert to alias, then determine its file URL and store that in variable "myFileUrl"
tell application "System Events" to set myFileUrl to URL of (sel as alias)

注意:

  • 示例仅在Finder选择恰好包含1个项目时有效。
  • tell application "System Events"部分是必不可少的,因为只有系统事件字典包含具有URL属性的别名类的类型
  • 为简洁起见,这两个陈述可以合并为一个。

现在,假设您要创建一个OS X服务,将当前在Finder中选择的文件和/或文件夹的文件URL复制到剪贴板:

  • 运行Automator,选择文件>新的,并选择创建新服务。
  • 在“服务接收已选中”下,选择“文件或文件夹”,然后选择“输入”,选择“Finder.app”
  • 添加“运行AppleScript”操作。
  • 粘贴以下AppleScript代码:
-- Receives the select files/folders from the Finder and copies their file URLs to the clipboard.
-- If the selection comprises more than 1 item, the URLs copied are separated by LF characters.
on run {input, parameters}
    set allUrls to ""
    tell application "System Events"
        repeat with f in input
            -- Convert input file/folder to a "System Events" alias...
            set a to f as alias
            -- and determine the value of the "URL" property, which is the file URL.
            set thisUrl to URL of a
            -- Add the file URL to the overall result.
            if length of allUrls is 0 then
                set allUrls to thisUrl
            else
                set allUrls to allUrls & linefeed & thisUrl
            end if
        end repeat
    end tell
    -- Finally, copy the file URL(s) to the clipboard.
    set the clipboard to allUrls
end run
  • 使用描述性名称保存新服务;例如,“将文件URL复制到剪贴板”

当您在Finder中按住Control键单击项目时,这将使新服务显示在上下文菜单中(取决于在上下文菜单的顶级或“服务”子菜单中定义的服务的数量)。

如果要为新服务指定键盘快捷键,请打开“系统偏好设置”,然后转到“键盘”按钮;键盘快捷键>服务,并在那里找到您新创建的服务。单击条目右边缘附近,然后按所需的组合键。

答案 3 :(得分:1)

好的,如果您使用 Finder 进行选择,您就不需要(" python"或"系统事件")来获取网址

因为您可以直接从Finder获取网址

tell application "Finder" to URL of item 1 of (get selection)
set the clipboard to the result

答案 4 :(得分:0)

AppleScript编辑器已发展成为脚本编辑器,除了AppleScript之外,它现在还提供自动化JavaScript(JXA)。

脚本编辑器通过WebKit JavaScriptCore框架提供encodeURI()和decodeUIR()。 encodeURI()和decodeUIR()极大地简化了Finder URL的解码和编码。

脚本编辑器试用示例

var appFinder = Application('Finder')

var url = appFinder.insertionLocation().url()
console.log("encoded : " + url)
console.log("decoded : " + decodeURI(url))

var urlList = appFinder.selection();
for (idx in urlList) {
  console.log("encoded : " + urlList[idx].url())
  console.log("decoded : " + decodeURI(urlList[idx].url()))
}

/*  encoded : file:///Volumes/Some%20HD/%E2%80%A2UnicodeName/file.ext */
/*  decoded : file:///Volumes/Some HD/•UnicodeName/file.ext */

注意:自动化JavaScript(JXA)首次推出10.10 OS X Yosemite。