四、AppleScript 操作Mac路徑

選擇文件夾,拿到文件夾路徑
choose folder

此時我選擇 test文件夾,那么結(jié)果區(qū)會顯示


5BD39F99-9AF4-4459-AF65-65F26E2AEAF4.png

由此發(fā)現(xiàn),路徑的格式為 硬盤:文件夾:子文件夾:子文件夾

打開文件夾 open folder
tell application "Finder"
    open folder "Macintosh HD:Users:lujh:Desktop:test:test:"
end tell
打開文件 open file
tell application "Finder"
    open file "Macintosh HD:Users:lujh:Desktop:test:test:AppDelegate.m"
end tell
或者使用 open alias
tell application "Finder"
    open alias "Macintosh HD:Users:lujh:Desktop:test:test:AppDelegate.m"
end tell
給一個變量賦一個路徑
tell application "Finder"
    set thePath to file "Macintosh HD:Users:lujh:Desktop:test:test:AppDelegate.m"
end tell
8121010C-CE76-424B-ABE5-2ABFAA3E739C.png
477577BE-C0C4-45ED-B68B-813F0B1C70A6.png

看到的結(jié)果并不是我們傳入的那個路徑格式

加上"a reference to"指令,使用這種路徑格式。
tell application "Finder"
    set thePath to a reference to file "Macintosh HD:Users:lujh:Desktop:test:test:AppDelegate.m"
end tell
選擇文件,拿到文件路徑
choose file
64D8772D-7A93-48EE-A795-DC3F59ECD01B.png

顯示的仍然是alias 替身。

假如我在桌面上為“ABC”文件夾中的文件“report.txt”創(chuàng)建了一個替身。如果今后我 移動“report.txt”到其它位置,或者把它重命名為“funny_story.txt”,雙擊替身依然能夠打開這 個文件。這是因為替身并不以“Macintosh HD:Users:lujh:Desktop:ABC:record.txt”的方式記錄文件 “report.txt” 的存儲位置(和名字),而是記錄這個文件的ID。Finder有專門的數(shù)據(jù)庫,里面保存 著代表具體文件的ID和當前文件所在的位置。當我移動文件“report.txt”時,代表這個文件的ID不 變,F(xiàn)inder只是更新它的數(shù)據(jù)庫,重新映射文件的新地址。當雙擊替身圖標,F(xiàn)inder就通過替身提供 的文件ID找到并打開具體的文件。

為了避免因為文件被移動或改名造成的腳本運行中斷,我們應(yīng)當讓腳本記錄文件的ID而不是 “符號鏈接”的路徑。

tell application "Finder"
    set thePath to alias "Macintosh HD:Users:lujh:Desktop:test:test:AppDelegate.m"
end tell
F3F8CD35-2176-4EA0-8648-4B7E1428880D.png
  • 將POSIX轉(zhuǎn)換為AppleScript路徑 POSIX file

set a to "/Users/lujh/MobSDKTool/MobSDKTool.xcodeproj"
set b to POSIX file a as string
  • 將AppleScript路徑轉(zhuǎn)成POSIX路徑 POSIX path of


文件操作

移動文件
tell application "Finder"
    move file "Macintosh HD:Users:lujh:Desktop:test.h" to trash
end tell
A3487991-56B1-4D94-BE73-FFF7D4D9101C.png
讀取文件
tell application "Finder"
    set thePath to alias "Macintosh HD:Users:lujh:Desktop:ABC:record.txt"
    read thePath
end tell
E51CF694-4C91-42C0-98F0-7B0683417421.png
創(chuàng)建文件夾
tell application "Finder"
    make new folder at desktop
end tell
C780E615-C509-4088-B56A-84755F08DB03.png
創(chuàng)建100個文件夾
tell application "Finder"
    make new folder at desktop with properties {name:"TTT"}
    -- 循環(huán)
    repeat with a from 1 to 100
        make new folder at folder "TTT" of desktop with properties {name:a as string}
    end repeat
end tell
272B2FFE-6BA8-498C-A014-C723BE0E2F34.png
獲取FInder 文件列表
tell application "Finder"
    -- 每種獲取的都是不同的
    every file of desktop
    files of desktop
    every folder of desktop
    folders of desktop
    name of every file of desktop
end tell
提取符合條件的文件夾
tell application "Finder"
    every file of desktop whose name contains "cu"
end tell
15BCCE87-7929-4995-B008-2C90C04295CB.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容