网上的几个applescript例子

本文介绍了一个AppleScript脚本,用于监控Adobe Illustrator应用程序的活动状态和CPU使用情况。当Illustrator处于非活动状态超过一定时间时,脚本会提示用户确认是否继续运行该程序,以避免不必要的资源占用。
清空idle进程
property ticsSinceActive : 0
property idleTime : 60 -- time to idle, in seconds
property timeLimit : 10 -- memory range, in minutes
property cpuUsage : {}

on idle
set maxTicCount to timeLimit * 60 / idleTime
tell application "System Events"
if exists (some process whose name contains "Illustrator") then
-- get reference to the app
set theIllustratorApp to some process whose name contains "Illustrator"
else
-- illustrator is not running
set ticsSinceActive to 0
set cpuUsage to {}
return
end if
set illustratorPID to unix id of theIllustratorApp
-- check if illustrator is inactive
if frontmost of theIllustratorApp is true then
set ticsSinceActive to 0
else
set ticsSinceActive to ticsSinceActive + 1
end if
-- take a running snapshot of cpu usage
set shellCmd to "ps -c -o %cpu='' -p " & illustratorPID
set end of cpuUsage to (do shell script shellCmd) as number
if (count of cpuUsage) is greater than maxTicCount then
-- trim off items more than 10 minutes old
set cpuUsage to items ((count of cpuUsage) - maxTicCount + 1) thru -1 of cpuUsage
end if
set maxCpu to 0
set aveCpu to 0
repeat with thisBit in cpuUsage
if thisBit > maxCpu then setmaxCpu to thisBit
set aveCpu to aveCpu + thisBit
end repeat
set aveCpu to aveCpu / (count of cpuUsage)
if ticsSinceActive > maxTicCount or maxCpu < 3 or aveCpu < 3 then
tell theIllustratorApp
activate
display alert "Shutting down Illustrator" message "Illustrator has been inactive for 10 minutes. Please verify that you are still using this application, or the system will shut it down." as critical buttons {"Close it", "Keep it running"} default button 1 giving up after 30
if button returned of the result is "Close it" or gave up of the result is true then
repeat 60 times
if exists document 1 then
close document 1 saving no
else
exit repeat
end if
end repeat
quit
else
set ticsSinceActive to 0
end if
end tell
end if
end tell
return idleTime
end idle


set backupFolder to (choose folder)
tell application "Finder" to set theseFolders to (get folders of backupFolder)
repeat with oneFolder in theseFolders
if (creation date of (info for oneFolder as alias) < (current date) - 30 * days) then
tell application "Finder" to delete oneFolder
end if
end repeat


检查程序是否运行
[code]global wasLoaded

on run
set wasLoaded to isAppLoaded("Safari")

idle
end run

on idle
set x to isAppLoaded("Safari")
if x and not wasLoaded then
display dialog "Safari was loaded."
set wasLoaded to true
else if wasLoaded and not x then
display dialog "Safari was quit."
set wasLoaded to false
end if
return 1 --will wait 1 second before checking again
end idle

on isAppLoaded(app_name)
tell application "System Events"
set app_list to every application process whose name contains app_name
if the (count of app_list) > 0 then
return true
else
return false
end if
end tell
end isAppLoaded
[/code]

自动把解压的文件放到Movie目录去
[code]on adding folder items to this_folder after receiving added_items

delay 0.2

set this_folder to alias "Liz's MBP:Users:liz:Downloads:Extract:"

set target_folder to alias "Liz's MBP:Users:liz:Movies:"

set fileList to {}

set fileList to my recursiveSearch(fileList, this_folder)

tell application "Finder"

repeat with aItem in fileList

if name extension of aItem is in {"avi", "wmv", "srt", "idx", "sub", "mkv"} then

if aItem exists then

move aItem to target_folder

end if

end if

end repeat

end tell

end adding folder items to


on recursiveSearch(theList, currentFolder)

tell application "Finder"

set theList to theList & (every file of currentFolder)

set folderList to (every folder of currentFolder)

repeat with newFolder in folderList

set theList to my recursiveSearch(theList, newFolder)

end repeat

return theList

end tell

end recursiveSearch[/code]

删除空文件夹
[code]--script to find empty folders and delete them

set pathoffolder to alias "Liz's MBP:Users:liz:Downloads:Extract:"

--change folder here

tell application "Finder"

repeat with oneFolder in (get folders of pathoffolder)

if (count items) of oneFolder is 0 or ((count items) of oneFolder is 1 and name of item 1 of oneFolder is ".DS_Store") then delete oneFolder

end repeat

end tell[/code]
<think>嗯,用户想知道在AppleScript中如何使用减法运算符,需要提供一个示例。首先,我需要回忆一下AppleScript的基本语法,特别是算术运算符的部分。记得AppleScript和其他编程语言类似,使用减号“-”作为减法运算符。 接下来,我应该确认一下AppleScript的语法结构,比如如何声明变量,如何执行运算。AppleScript中使用set关键字来赋值变量,然后可以直接进行数学运算。比如,set result to 10 - 5,这样结果应该是5。 然后,用户可能需要更具体的例子,比如两个变量相减,或者更复杂的表达式。比如,先定义两个变量a和b,然后相减,输出结果。此外,可能还需要提到AppleScript中运算符的优先级,比如先乘除后加减,但用户的问题主要集中在减法,所以这部分可能可以简要带过,但如果有引用资料中提到运算符优先级的话,需要结合引用内容。 查看用户提供的引用内容,引用[2]提到了一元负运算符的优先级高于二元运算符,这在AppleScript中可能也有类似的情况,不过需要确认。不过AppleScript和CachéObjectScript不同,但可以提醒用户注意运算符的优先级,特别是在复杂表达式中使用括号来明确顺序。 然后,用户需要示例代码。应该给出几个不同的例子,比如简单的数字相减,变量相减,以及可能涉及负数的情况。例如: 第一个例子直接计算10减5,第二个例子用变量存储数值再相减,第三个例子可能展示更复杂的运算,如带括号的表达式,或者与其他运算符结合使用。 另外,考虑到用户可能对错误处理或特殊案例感兴趣,比如处理非数字类型时的错误,可以提到AppleScript中的类型检查和错误提示,但如果没有相关资料的话,可能不需要深入,保持回答简洁。 最后,确保回答结构清晰,分步骤说明,并引用相关的引用资料。例如,在提到运算符优先级时引用引用[2],但要注意引用是否适用于AppleScript。不过引用[2]是关于CachéObjectScript的,所以可能不能直接适用,但可以类比说明,或者不引用,直接说明AppleScript的情况。如果引用中的内容不相关,可能需要忽略,避免误导用户。 总结回答的结构应该是:先介绍减法运算符的符号,然后给出基本示例,变量示例,复杂表达式示例,并提醒运算符优先级和括号的使用。最后生成相关问题,帮助用户进一步学习。</think>### AppleScript 减法运算符使用说明 在AppleScript中,减法运算符使用标准的减号 `-` 实现数值相减操作,支持整数、浮点数及变量间的运算。 #### 基础示例 1. **直接数值运算** ```applescript set result to 10 - 5 -- 结果为5 ``` 2. **变量间运算** ```applescript set a to 20 set b to 7 set difference to a - b -- 结果为13 ``` 3. **复合表达式** ```applescript set total to (15 * 3) - (8 + 4) -- 先计算括号内结果,再相减:45 - 12 = 33 ``` #### 注意事项 - **运算符优先级**:乘除优先于加减,建议用括号明确顺序[^2]。例如: ```applescript set value to 10 - 3 * 2 -- 结果为4(先计算3*2=6,再10-6) ``` - **负数处理**:可直接用负号定义负数: ```applescript set result to -5 - 10 -- 结果为-15 ``` - **类型检查**:AppleScript要求操作数为数值类型,否则会报错: ```applescript set textVar to "10" set result to textVar - 5 -- 错误:无法对文本执行减法 ``` --- 相关问题
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值