双击时,这个小的AppleScript会从.webloc文件中读取URL。但是当在脚本上删除相同的.webloc文件时,相应的网站将在默认浏览器中打开,脚本将无法执行。如何防止此行为,并按预期读取URL?
on run
open {choose file with prompt "Choose a file:" without invisibles}
end run
on open the_files
repeat with i from 1 to (count the_files)
if type identifier of (info for the_files) is "com.apple.web-internet-location" then
# "read file" can only access the data fork; the URL is stored inside the resource fork
tell application "Finder" to set weblocURL to the location of (the_files as alias)
set the clipboard to weblocURL
else
set the clipboard to "bummer"
end if
end repeat
end open
此脚本有效:
on run
open {choose file with prompt "Choose a file:" without invisibles}
end run
on open location this_URL
set the clipboard to this_URL
end open location
on open the_files
repeat with i from 1 to (count the_files)
try
if type identifier of (info for (item i of the_files)) is "com.apple.web-internet-location" then
# "read file" can only access the data fork; the URL is stored inside the resource fork
tell application "Finder" to set weblocURL to the location of (item i of the_files)
set the clipboard to weblocURL
else
set the clipboard to "bummer"
end if
end try
end repeat
end open
答案 0 :(得分:0)
这只是一个猜测...我怀疑这种类型的文件会发出一个"开放的位置"系统范围的通知和Safari响应该通知。因此,我建议您考虑修改脚本以响应"打开位置"。快速谷歌搜索出现了this instruction,这可以让您了解需要更改的内容。
您的代码中也有一些错误。在你的重复循环中,任何时候你使用" the_files"你需要用"(the_files的第i项)"替换它。原因是" the_files"是一个文件列表,因此您有一个重复循环来处理该列表中的每个项目。因此,您必须在重复循环内对项目i执行操作,以将实际文件从列表中删除。
同样在你的"告诉应用程序查找器"你应该删除"作为别名"。 the_files的项目i已经是别名,因此您无需将其强制转换为别名。
祝你好运。