【问题标题】:How to get Applescript input to run Terminal Command如何获取 Applescript 输入以运行终端命令
【发布时间】:2013-08-28 20:49:35
【问题描述】:

我经常在学校计算机之间切换,并且喜欢显示隐藏文件,但是,并不是每个人都这样做。通常我使用

"defaults write com.apple.finder AppleShowAllFiles -bool true"

命令,但是,如果我可以运行一些 Applescript,而不是手动将文本复制到终端,然后在完成后重做,那将非常方便。所以我想要完成的是接收用户关于他们是否要显示所有文件的输入,然后运行该命令。对 Applescript 进行了一些初步研究,我能够弄清楚我将如何构建它的一些基本想法。下面的代码是错误的,所以请大家见谅。

(choose from list {"Hide", "Show"} ¬
    with prompt "Do you want to hide or show hidden files?")
if "Hide" then
    do shell script "defaults write com.apple.finder AppleShowAllFiles -bool False"
else 
    do shell script "defaults write com.apple.finder AppleShowAllFiles -bool True"
end 

我可以打开用户对话框,但是,当我尝试输入选项时,它会回复:“无法将“隐藏”转换为布尔类型。”。如果有人可以帮助我向我展示我需要更改的内容,那将不胜感激。

谢谢,迈克尔。

【问题讨论】:

    标签: macos applescript


    【解决方案1】:

    choose from list 返回所选项目的列表。

    choose from list {"Hide", "Show"} with prompt "Do you want to hide or show hidden files?"
    if result is {"Show"} then
        do shell script "defaults write com.apple.finder AppleShowAllFiles -bool true"
    else
        do shell script "defaults write com.apple.finder AppleShowAllFiles -bool false"
    end if
    quit application "Finder"
    

    我使用这样的脚本来切换显示隐藏文件:

    do shell script "x=$(defaults read com.apple.finder AppleShowAllFiles)
    [ $x = 1 ] && b=false || b=true
    defaults write com.apple.finder AppleShowAllFiles -bool $b"
    tell application "Finder"
        quit
        delay 0.1 -- without this delay Finder was not made frontmost
        launch -- open Finder in the background
        delay 0.1 -- without this delay there was sometimes a "connection is invalid" error
        activate -- make Finder frontmost
        reopen -- open a new default window
    end tell
    

    【讨论】:

    • 非常感谢您的仓促回复,您对我的代码所做的更改似乎效果很好。
    【解决方案2】:

    从列表中选择返回一个列表(如果用户取消,则返回 False)。这样做可以将列表强制转换为字符串:

    (choose from list {"Hide", "Show"} ¬
        with prompt "Do you want to hide or show hidden files?") as string
    

    请确保保留括号,否则as string 将强制转换您的提示字符串,您仍会收到一个返回列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多