【问题标题】:How to reveal default Safari downloads folder through Applescript?如何通过 Applescript 显示默认的 Safari 下载文件夹?
【发布时间】:2015-07-03 02:43:21
【问题描述】:

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

---我已经尝试过的东西---

设置 filePath 以执行 shell 脚本“默认读取 com.apple.Safari 下载路径” 执行 shell 脚本 "open" & 引用文件路径的形式

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

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

【问题讨论】:

  • Safari 13 似乎损坏了defaults write com.apple.safari DownloadsPath。知道新的偏好是什么吗?

标签: macos safari applescript


【解决方案1】:

基于@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

textelement 用于从主文件夹 (/Users/johndoe/) 中删除尾随 /

rest property 返回folderPath (~/Downloads) 中~ 之后的每个字符。

/Users/johndoe + /Downloads = /Users/johndoe/Downloads

【讨论】:

  • 这就是提醒我们AppleScript并不是某些事情的最佳选择。 :)
  • 只是想了解这条线的作用,你能解释一下吗?我得到了你写的所有其他东西,只是无法得到这个:“将文件夹路径设置为文本 1 到 -2”
  • 啊,明白了!基本上第一个数字代表从左边的修剪(0实际上是= 1)和右边的修剪(0实际上是= -1)。所以我们正在修剪尾部的斜线
【解决方案2】:

您可以通过 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

【讨论】:

  • 没有朋友只在 Safari 中打开下载列表,而不是在 Finder 中打开选定的默认下载文件夹
【解决方案3】:

这是一个使用 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

【讨论】:

  • 谢谢!我们快到了,唯一的问题是它似乎正在用正斜杠替换有空格的项目。 4 例如,如果我有一个名为“我的文件夹”的文件夹,则路径将打印为“.../my/folder/”
  • 我知道了,William,非常感谢您抽出宝贵时间!度过美好的一天!
【解决方案4】:

我想你想要的是这样的:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多