Joh*_* Le 3 shell applescript progress
我正在创建一个AppleScript,它在Mac Os X中创建/ Users文件夹的备份图像.我想做两件事之一:
我尝试使用/ 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)
使用板载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用户确认,因为它非常强调消息的第一部分.一起意味着您的代码可以修改为:
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)
| 归档时间: |
|
| 查看次数: |
8239 次 |
| 最近记录: |