Applescript
文章目录
一、简述 Applescript
Applescript 是 Apple 开发根据 Apple events 来做 应用间通信(IAC : inter-application communication )的脚本语言。
相关语言特性
- 面向对象
- 采用Unicode文字编码,不区分大小写
OSA
https://www.oreilly.com/library/view/applescript-in-a/1565928415/ch01s02s01.html
The Open Scripting Architecture, which has been present on the Mac since the early 1990s, is Apple Computer’s mechanism for making interapplication communication available to a spectrum of scripting languages. AppleScript is the OSA language that Apple provides, but there are other OSA-compliant languages, including UserLand Frontier and JavaScript.[2]
OSA accomplishes this “the-more-the-merrier” approach to scripting systems by using Apple events as the unifying technology. The situation is similar to Open Database Connectivity (ODBC) on the Windows platform, where any client application can talk to different database management systems using ODBC as a common conduit (as long as ODBC driver software exists for that particular database). In terms of OSA, the conduit (on Mac OS 9) is a scripting component that can convert whatever scripting language is used (AppleScript or JavaScript) into one or more properly constructed Apple events. Figure 1-3 shows the same Apple event being sent to an application in two different scripting languages.
OSA: Open Scripting Architecture
AppleScript 是 Apple 提供的 OSA 语言;
二、编写工具
ps:系统环境:10.13.6
- 脚本编辑器;地址位于
/Applications/Utilities/Script Editor.app
- 自动操作;
/Applications/Automator.app
脚本编辑器的使用
1、便捷调用命令
点击上方按钮,自动生成下面的代码
2、编写一个简单的脚本
在桌面创建文件夹
可以保存为以下格式
三、osascript 命令
osascript – execute OSA scripts (AppleScript, JavaScript, etc.)
osascript 是用来执行 OSA 脚本的(包含 AppleScript, JavaScript 等等)。
执行地址: /usr/bin/osascript
方法选项:osascript [-l language] [-i] [-s flags] [-e statement | programfile] [argument ...]
osascript 使用举例
- 运行一句脚本:
osascript -e 'display dialog "hello world"'
- 运行脚本文件:
osascript PATH-TO-SCRIPT.scpt
其他相关命令
- osalang
- osacompile
四、Cocoa 相关类
- NSAppleScript
- NSAppleEventDescriptor
使用 NSAppleScript
在 AuthorizationExecuteWithPrivileges 方法过期的情况下,用 NSAppleScript 执行高权限命令是一个不错的方法:
+ (AppleScriptReturn *)runAppleScript:(NSString *)command previledged:(BOOL)previledged{
NSString *script = [NSString stringWithFormat:@"do shell script \"%@\"", command];
if (previledged) {
script = [script stringByAppendingString:@" with administrator privileges"];
}
NSAppleScript *appleScript = [[NSAppleScript new] initWithSource:script];
NSDictionary *scriptError = [NSDictionary new];
NSAppleEventDescriptor *descriptor = [appleScript executeAndReturnError:&scriptError];
NSLog(@"runAppleScript : %@ , scriptError : %@",descriptor.stringValue,scriptError);
AppleScriptReturn *returnModel = [[AppleScriptReturn alloc]init];
if(scriptError.allKeys.count > 0) {
returnModel.ret = NO;
returnModel.result = [scriptError objectForKey:NSAppleScriptErrorMessage];
} else {
NSAppleEventDescriptor *unicode = [descriptor coerceToDescriptorType:typeUnicodeText];
NSData *data = [unicode data];
NSString *result = [[NSString alloc] initWithCharacters:(unichar*)[data bytes] length:[data length] / sizeof(unichar)];
returnModel.ret = YES;
returnModel.result = result;
}
NSLog(@"%d - %@",returnModel.ret,returnModel.result);
return returnModel;
}
五、查看应用对 Applescript 的支持
六、让自己的应用支持 Applescript
– Applescript support to my Cocoa application?
https://stackoverflow.com/questions/2479585/how-do-i-add-applescript-support-to-my-cocoa-application
七、Python 调用 Applescript
使用库 applescript
PYPI : https://pypi.org/project/applescript/
import applescript
r = applescript.run('return 1')
# 运行脚本
r = applescript.run('path/to/file.applescript')
# 打开终端运行脚本
applescript.tell.app("Terminal",'do script "ls"')
# 后台运行脚本
applescript.tell.app("Terminal",'do script "ls"',background=True)
# 运行多个组合脚本
command="cd ~/Downloads/figures && srcana && ls"
applescript.tell.app("Terminal",f'''do script "{command}"''')
八、一些编写示例
在 Safari 新窗口中打开网页
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"https://blog.csdn.net/lovechris00/"})
end tell
end tell
将当前 safari 页面切换为指定页面
tell application "Safari" to set the URL of the front document to "https://blog.csdn.net/lovechris00/"
通过终端执行命令 echo ‘hello’
tell application "Terminal"
do shell script "echo 'hello' "
end tell
打开终端执行python文件
在 python 中调用
python_path = '~/opt/anaconda3/bin/python3.7'
script_path = '~/code/detail.py'
s1 = f'''
tell application "Terminal"
set newTab to do script "{python_path} {script_path} 00 "
end tell
'''
ret = applescript.run(s1)
参考
- AppleScript Language Guide
https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html#//apple_ref/doc/uid/TP40000983-CH218-SW2 - Open Scripting Architecture Reference
https://applescriptlibrary.files.wordpress.com/2013/11/open-scripting-architecture.pdf - Wiki-AppleScript
https://en.wikipedia.org/wiki/AppleScript - AppleScript语言介绍(译文)
https://www.jianshu.com/p/617b443437fb - AppleScript 入门:探索 macOS 自动化
https://sspai.com/post/46912 - How Mac Scripting Works
https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/HowMacScriptingWorks.html - Mac Automation Scripting Guide
https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/HowMacScriptingWorks.html