Applescript Shell脚本进展

Joh*_* Le 3 shell applescript progress

我正在创建一个AppleScript,它在Mac Os X中创建/ Users文件夹的备份图像.我想做两件事之一:

  1. 在shell脚本运行时显示理发杆进度条,并选择退出.
  2. 在脚本运行时显示一个对话框,并带有退出选项.

我尝试使用/ dev/null执行shell脚本但忽略所有输出.我想保存输出并将其显示在用户的对话框中.

这是我的代码:

set computername to (do shell script "networksetup -getcomputername")
set fin to ""
set theIcon to note
tell application "Finder"
    try
        set folderalias to "/Users"
        set foldersize to physical size of folder (alias ":Users") --(info for folderalias) 
        set foldersize to (round ((foldersize / 1.0E+9) * 100)) / 100
    on error
        tell application "System Events"
            set foldersize to (round (((get physical size of folder ":Users" as text) / 1.0E+9) * 100)) / 100
        end tell
    end try
end tell

display dialog "User profile backup utility" & return & return & ¬
    "The computer name is: " & computername & return & return & ¬
    "The '/Users/' directory size is: " & "~" & foldersize & " GB" & return & return & ¬
    "Would you like to backup the '/User' directory now?" & return ¬
    buttons {"Cancel", "Backup Now"} default button "Backup Now"
set comd to "hdiutil create ~/Desktop/" & computername & ".dmg -srcfolder /test/"
set fin to do shell script (comd) with administrator privileges
display dialog fin
Run Code Online (Sandbox Code Playgroud)

kop*_*hke 5

使用板载AppleScript(即标准添加)无法显示进度条对话框,但这可以使用Shane Stanley的ASObjC Runner实现,ASObjC Runner是一个可编写脚本的无面背景应用程序,它在许多有用的功能中提供了一组相关的进度对话框命令.下载到您的系统后,

tell application "ASObjC Runner"
    reset progress
    set properties of progress window to {button title:"Abort Backup", button visible:true, message:"Backing up the '" & (POSIX path of folderalias) & "' directory.", detail:"There are " & foldersize & " GB of data to backup – this might take some time.", indeterminate:true}
    activate
    show progress
end tell
try -- to make sure we destroy the dialog even if the script error out
    <your backup operation here>
end try   
tell application "ASObjC Runner" to hide progress
Run Code Online (Sandbox Code Playgroud)

在备份操作运行时将显示一个不确定的进度条(或"理发杆") - 至少如果它是同步的(因为从AS调用的shell命令).至于你的shell命令的输出,它由do shell script命令自动返回- 在你的代码中,它被分配给fin[代码从ASObjC Runner文档中提取或多或少批量].

ASObjC Runner可以嵌入到AppleScript应用程序中(将脚本保存为AppleScript编辑器中的应用程序),方法是将其放入bundle的Resources文件夹中(在Finder中,在上下文菜单中选择Show Package Contents)并使用path to resource命令using terms from内调用它- 我上面链接的文档包含详细信息和示例代码,但最重要的是,它包含一个严重错误:您的tell语句需要使用到Resources bundle(tell application (POSIX path of (path to resource "ASObjC Runner.app")))的POSIX路径.

关于您的代码的一些评论:

  • 有一种更优雅的方式来获取/Users文件夹的别名:

    path to users folder
    
    Run Code Online (Sandbox Code Playgroud)

    - 无需硬连线和调用Finder.然后,您可以通过使用POSIX path of,或者,如果您需要引用它,获得quoted form of POSIX path of与它的shell兼容路径.

  • 我建议只使用系统事件来获取physical size- 与Finder不同,它在后台运行.这将允许你摆脱tell application "Finder"try … catch阻止(不知道你的意图是什么,无论如何 - 如果你的反应error -10004,这是因为round不喜欢被放在一个tell块内).
  • 无需初始化fin为空字符串 - 您将从中获取返回值do shell script.
  • 说到你的do shell script,你需要引用computerName它的变量来打破那个名字的空格.
  • theIcon 从未使用过.
  • 您可能希望使用display alert而不是display dialog用户确认,因为它非常强调消息的第一部分.
  • 代码中有许多不必要的括号 - AppleScript需要其中一些来分隔语义命令块,但不是所有这些都是...

一起意味着您的代码可以修改为:

set computerName to do shell script "networksetup -getcomputername"
set folderAlias to path to users folder
tell application "System Events" to set folderSize to physical size of folderAlias
set folderSize to round(folderSize / 1.0E+9 * 100) / 100

display alert "User profile backup utility" message ¬
  "The computer name is: " & computerName & return & return & ¬
  "The '" & (POSIX path of folderAlias) & "' directory size is: " & "~" & folderSize & " GB" & return & return & ¬
  "Would you like to backup the '" & (POSIX path of folderAlias) & "' directory now?" & return ¬
  buttons {"Cancel", "Backup Now"} default button "Backup Now"
set shellCmd to "hdiutil create ~/Desktop/'" & computerName & "'.dmg -srcfolder /test/"
-- progress bar dialog display initialization goes here
try
    set shellOutput to do shell script shellCmd with administrator privileges
end try
-- progress bar dialog hiding goes here
display dialog shellOutput
Run Code Online (Sandbox Code Playgroud)