选择文件夹,拿到文件夹路径
choose folder #会弹出文件窗口选择,选择一个文件夹会返回路径
此时我选择 test文件夹,那么结果区会显示
image.png
由此发现,路径的格式为 硬盘:文件夹:子文件夹:子文件夹
打开文件夹 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
image.png
image.png
看到的结果并不是我们传入的那个路径格式
加上"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
image.png
显示的仍然是alias 替身。
假如我在桌面上为“ABC”文件夹中的文件“report.txt”创建了一个替身。如果今后我 移动“report.txt”到其它位置,或者把它重命名为“funny_story.txt”,双击替身依然能够打开这 个文件。这是因为替身并不以“Macintosh HD:Users:lujh:Desktop:ABC:record.txt”的方式记录文件 “report.txt” 的存储位置(和名字),而是记录这个文件的ID。Finder有专门的数据库,里面保存 着代表具体文件的ID和当前文件所在的位置。当我移动文件“report.txt”时,代表这个文件的ID不 变,Finder只是更新它的数据库,重新映射文件的新地址。当双击替身图标,Finder就通过替身提供 的文件ID找到并打开具体的文件。
为了避免因为文件被移动或改名造成的脚本运行中断,我们应当让脚本记录文件的ID而不是 “符号链接”的路径。
tell application "Finder"
set thePath to alias "Macintosh HD:Users:lujh:Desktop:test:test:AppDelegate.m"
end tell
image.png
将POSIX转换为AppleScript路径 POSIX file
set a to "/Users/coffee/Desktop/iOS练习代码/WeChatDemo/WeChatDemo.xcodeproj"
set b to POSIX file a as string
将AppleScript路径转成POSIX路径 POSIX path of
set a to "OXS:Users:coffee:Desktop:iOS练习代码:WeChatDemo:WeChatDemo.xcodeproj"
set b to POSIX path of a as string
image.png
文件操作
- 移动文件
tell application "Finder"
move file "Macintosh HD:Users:lujh:Desktop:test.h" to trash
end tell
image.png
- 读取文件
tell application "Finder"
set thePath to alias "Macintosh HD:Users:lujh:Desktop:ABC:record.txt"
read thePath
end tell
image.png
- 创建文件夹
tell application "Finder"
make new folder at desktop
end tell
image.png
- 创建100个文件夹
tell application "Finder"
make new folder at desktop with properties {name:"TTT"}
-- 循环
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
image.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
image.png











网友评论