我是AppleScript的新手,想知道如何通过AppleScript在Safari中显示默认下载文件夹。
---我已经做过什么---
设置filePath做shell脚本"默认读取com.apple.Safari DownloadsPath" 做shell脚本"打开" &安培;引用的filePath形式
我理解的上述脚本将设置变量" filePath"到Safari的偏好中的默认PList条目。 只要位置不在用户主文件夹中的某个位置,这就很有用。如果它在用户文件夹中,日志会显示一个"〜/"在路径之前没有引用用户主文件夹之前的任何内容(相对路径)
我如何实现目标?有没有办法获得文件夹的绝对路径?或者也许是另一种方法?
答案 0 :(得分:2)
基于@ WilliamTFroggard的回答:
tell application "Finder"
set folderPath to do shell script "defaults read com.apple.Safari DownloadsPath"
if folderPath = "~" or folderPath starts with "~/" then ¬
set folderPath to text 1 thru -2 of POSIX path of (path to home folder) & rest of characters of folderPath
open POSIX file folderPath as alias
activate
end tell
text
element用于从主文件夹(/
)中删除尾随的/Users/johndoe/
。
rest
property会返回~
folderPath
中~/Downloads
后面的每个字符。
/Users/johndoe
+ /Downloads
= /Users/johndoe/Downloads
。
答案 1 :(得分:1)
这是使用Python的方法:
do shell script "python -c \"import os; print os.path.expanduser('`defaults read com.apple.Safari DownloadsPath`')\""
如果你真的想要长AppleScript方法,试试这个:
set filePath to ""
set AppleScript's text item delimiters to "/"
set filePathWords to words of (do shell script "defaults read com.apple.Safari DownloadsPath")
if item 1 of filePathWords is "~" then
set item 1 of filePathWords to (POSIX path of (path to home folder as string))
set filePath to filePathWords as string
else
set filePath to "/" & filePathWords as string
end if
如果路径中的第二个“/”困扰你(这有点困扰我......),你可以改用它:
set filePath to ""
set AppleScript's text item delimiters to "/"
set filePathWords to words of (do shell script "defaults read com.apple.Safari DownloadsPath")
if item 1 of filePathWords is "~" then
set item 1 of filePathWords to (do shell script "cd ~ && pwd")
set filePath to filePathWords as string
else
set filePath to "/" & filePathWords as string
end if
答案 2 :(得分:1)
您可以通过Applescript GUI Scripting执行此操作,使Applescript从“视图”菜单中单击“显示下载”选项:
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari"
tell menu bar 1
tell menu bar item "view"
tell menu "View"
click menu item "Show downloads"
end tell
end tell
end tell
end tell
end tell
答案 3 :(得分:1)
我认为你想要的是:
tell application "Finder"
activate
open ("/Users/yourname/Downloads" as POSIX file)
end tell
只需更换" yourname"在Finder中以你的名字命名。