提问者:小点点

Applescript外壳程序脚本进度


我正在创建一个苹果脚本,用于在Mac OS X中创建/Users文件夹的备份映像。我想做以下两件事之一:

    < li >在shell脚本运行时显示barber pole进度条,并带有退出选项。 < li >在脚本运行时显示带有退出选项的对话框。

我尝试使用 /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

共2个答案

匿名用户

使用板载AppleScript(即标准添加)无法显示进度条对话框,但这可以使用Shane Stanley的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

将在备份操作运行时显示不确定的进度条(或“理发杆”)——至少如果它是同步的(就像从 AS 调用的 shell 命令一样)。至于 shell 命令的输出,它由 do shell 脚本命令自动返回 – 在您的代码中,它被分配给 fin [或多或少从 ASObjC Runner 文档中批量提取的代码]。

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

关于您的代码的几点备注:

>

  • 有一种更优雅的方法可以获取 /Users 文件夹的别名:

    path to users folder
    

    -不需要硬接线和调用Finder。然后,您可以使用的POSIX路径来获取shell兼容路径,或者,如果您需要引用它,引用它的的POSIX路径的形式。

    所有这些意味着您的代码可以修改为:

    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
    

  • 匿名用户

    虽然没有kopischke的建议那么漂亮,但获取进度信息的一种快速而肮脏的方法是在终端中运行命令。

    set comd to "hdiutil create -puppetstrings '~/Desktop/" & computername & ".dmg' -srcfolder '/test/'"
    tell application "Terminal" to do script comd