簡體   English   中英

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

[英]How to get the a file URL in OSX with AppleScript or a shell script?

我試圖破解一個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)但這對我沒用 - 也許我做錯了。

另一種選擇是編寫特殊字符的腳本 - 我也可以使用它,但我不確定要轉換成什么 - 否則我會尋找它。

這是一個簡單的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

這是從幾乎是逐字解除這個答案 ,即利用Python的urllib的引用字符串適當地添加在之前file://localhost的字符串的開始

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之間兼容。

有一個更強大和方便的解決方案(在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"部分是必不可少的,因為只有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鍵單擊項目時,這將使新服務顯示在上下文菜單中(取決於在上下文菜單的頂級或“服務”子菜單中定義的服務的數量)。

如果要為新服務分配鍵盤快捷方式,請打開“系統偏好設置”,轉到“鍵盤”>“鍵盤快捷方式”>“服務”,然后在其中找到新創建的服務。 單擊條目右邊緣附近,然后按所需的組合鍵。

好的,如果你使用Finder來獲取選擇,你不需要(“python”或“系統事件”)來獲取URL

因為您可以直接從Finder獲取URL

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

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM