Macos 如何通过Applescript显示默认Safari下载文件夹?

Macos 如何通过Applescript显示默认Safari下载文件夹?,macos,safari,applescript,Macos,Safari,Applescript,我是AppleScript新手,想知道如何通过AppleScript显示Safari中的默认下载文件夹 ---我已经试过了--- 设置filePath以执行shell脚本“默认读取com.apple.Safari DownloadsPath” 执行shell脚本“打开”并引用文件路径形式 据我所知,上述脚本将在首选项中将变量“filePath”设置为Safari的默认PList条目。 只要该位置不在用户主文件夹中,这种方法就非常有效。如果在用户文件夹中,日志会在路径前显示“~/”,而不引用用户主

我是AppleScript新手,想知道如何通过AppleScript显示Safari中的默认下载文件夹

---我已经试过了---

设置filePath以执行shell脚本“默认读取com.apple.Safari DownloadsPath” 执行shell脚本“打开”并引用文件路径形式

据我所知,上述脚本将在首选项中将变量“filePath”设置为Safari的默认PList条目。 只要该位置不在用户主文件夹中,这种方法就非常有效。如果在用户文件夹中,日志会在路径前显示“~/”,而不引用用户主文件夹(相对路径)之前的任何内容


我如何实现我的目标?有没有办法获得文件夹的绝对路径?或者是另一种方法?

这里有一种使用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

您可以通过Applescript GUI脚本使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

根据@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
文本
用于从主文件夹(
/Users/johndoe/
)中剥离尾随的
/

rest
返回
folderPath
~/Downloads
)中
~
后面的每个字符


/Users/johndoe
+
/Downloads
=
/Users/johndoe/Downloads
我想你想要的是:

tell application "Finder"
activate
open ("/Users/yourname/Downloads" as POSIX file)
end tell

只需在Finder中将“yourname”替换为您的姓名即可。

这提醒我们,AppleScript并不是解决某些问题的最佳选择。:)我只是想弄明白这句话的意思,你能解释一下吗?我得到了你写的所有其他东西,只是不能得到这个:“将folderPath设置为文本1到2”啊,明白了!基本上,第一个数字表示从左到右的修剪(0实际上等于1)和修剪(0实际上等于-1)。因此,我们正在修剪尾部的斜纹!我们就快到了,唯一的问题是它似乎在用正斜杠替换有空格的项目。4示例如果我有一个名为“我的文件夹”的文件夹,路径打印为“../my/folder/”我知道了William,非常感谢您抽出时间!祝你有一个美好的一天!没有朋友在Safari中打开下载列表,而不是FinderSafari 13中选择的默认下载文件夹,似乎已损坏
defaults write com.apple.Safari DownloadsPath
。你知道新的偏好是什么吗?