AppleScript 是 Apple 平台 用来操控系统及 app 的一种脚本语言, 简单使用时非常便利, 但是在一些灵活场景下便难以胜任, 这篇谈谈我遇到的 variable expansion 问题

himg

事件背景: EuDic 提供了 AppleScript 脚本控制功能, 我想要写一个 AppleScript 脚本来快速查找单词, 但是 EuDic 有 Pro / Lite 两种版本,

  • Pro

    • app name: Eudic.app
    • bundle id: com.eusoft.eudic
  • Lite

    • app name: Eudb_en_free.app
    • bundle id: com.eusoft.freeeudic

因此我必须在脚本中区分出用户安装的版本, 然后进行相应版本的调用

在脚本编写过程中, 我发现 AppleScript 在某些位置是不支持 variable expansion

-- script1.applescript
set appName to "EuDic"
tell application "System Events"tell application appNameactivateshow dic with word "hello"end tell
end tell
-- script2.applescript
tell application "System Events"tell application "EuDic"activateshow dic with word "hello"end tell
end tell

运行 script1 脚本会报错: script error: Expected end of line, etc. but found identifier. (-2741), 运行 script2.applescript 则完全没有问题, 这就让我感到很奇怪了, 难道一个 AppleScript 连 variable expansion 能力都没有? 经过了大量资料查找后, 我发现它真的没有这个能力...

因为 AppleScript 编译器采用了各种技巧来支持那些花哨的类英语关键字. 这些技巧中最主要的是寻找 tell application "..." 行, 这样它就知道在 tell 块中编译语句时要查找哪些特定于应用程序的关键字.

大多数情况下, 这对于简单的代码来说已经足够了, 但是一旦你想让你的代码更加灵活, 这种聪明反而会为你带来羁绊. 因为脚本直到运行时才提供应用程序名称, 编译器在编译时不知道查找该应用程序的术语, 因此只能使用 AppleScript 中预定义的那些关键字和任何加载的 osaxen.

在我们这个例子中, show dict with word 术语是由 EuDic 定义的, 但是直到运行时, AppleScript 才知道他要找的术语是 EuDic 提供的, 这时如果直接运行 show dic with word 术语, 那么就会报错(在这种情况下, activate 并不会报错, 因为 activate 是预定义的术语), 对于这种情况, 我在网上找到的解决办法大致如下:

  1. 直接使用原始 "com.eusoft.eudic"

  2. 将相关代码包含在 using terms from application ... 块中. 这明确告知编译器在编译所附代码时从何处获取附加术语.

    set appName to "EuDic"
    

tell application “System Events”
tell application appName
activate
using terms from application “EuDic”
show dic with word “hello”
end using terms from
end tell
end tell

很明显, 上面两种方式需要直接把 "EuDic" 写死, 那么到底有没有方法能在 AppleScript 中动态地 variable expansion 呢? 我想到了在 Shell 中调用 AppleScript 的方式. 根据 so 的回答, 我们有三种方式可以在 shell 中调用 AppleScript, 其中 Here Doc 方式是支持 variable expandsion 的, 因此我的方案就是 Shell + AppleScript + Here Doc

Shell 的 here doc 默认支持 variable expansion(当然, 我们可以使用引号 <<'EOF' 使该功能关闭), 具体实现如下:

#!/usr/bin/env bash

if [[ -d /Applications/Eudb_en_free.app ]]; then    eudicID=$(osascript -e 'id of app "Eudb_en_free"')elif [[ -d /Applications/Eudic.app ]]; then    eudicID=$(osascript -e 'id of app "Eudic"')fi

if [[ -z "$eudicID" ]]; thenosascript <<EOFdisplay dialog "Please install EuDic"EOF   exitfi

osascript <<EOFtell application "System Events"    do shell script "open -b $eudicID"    tell application id "$eudicID"        activate        show dic with word "$1"    end tellend tellEOF

这样, 我们便可以同时利用 AppleScript 的便利性与 Shell 的灵活性了.

这是目前我自己能想到的比较好的解决办法, 如果你有更好的方法可以留言交流 ✌️

Project

hanleylee/alfred-eudic-workflow

Ref

  • Passing variables into 'tell application' commands
  • Adding AppleScript to Bash Script
  • Using variables inside a bash heredoc

Variable Expansion in Applescript相关推荐

  1. [IAR] 编译报错:Variable expansion failed for Pre-Build command line

    这里写目录标题 项目场景: 问题描述: 原因分析: 解决方案: 项目场景: 导入工程,编译报错. Variable expansion failed for Pre-Build command lin ...

  2. ats 5.3.2中的header-rewrite插件调研

    用途 该插件允许你通过预先定义的规则来修改不同的headers.改造自Apache HTTPD的mod_rewrite模块. 源码目录 trafficserver-5.3.2/plugins/head ...

  3. What are some time-saving tips that every Linux us

    2019独角兽企业重金招聘Python工程师标准>>> Joshua Levy, Trust me. I'm a professional. Votes by Kartik Ayya ...

  4. 多行字符串,带有多余的空格(保留缩进)

    本文翻译自:Multi-line string with extra space (preserved indentation) I want to write some pre-defined te ...

  5. modelsim加入xilinx ISE库的方法

    文章目录 背景 方法 背景 由于ISE仿真用Isim虽然也行,但是用习惯了modelsim,还是用modelsim方便.为了避免每次都要重复编译xilinx的库,可以一次性将所有xilinx的库编译后 ...

  6. python magic文档

    输入 %magic Jupyter Notebook%magicIPython's 'magic' functions ===========================The magic fun ...

  7. jupyter notebook即原来的Ipython notebook的使用方法

    jupyter 的快捷键 ​IPython -- An enhanced Interactive Python - Quick Reference Card ===================== ...

  8. WebLogic11g-常用运维操作

    转自:https://dead-knight.iteye.com/blog/1940399 希望这篇能把weblogic运维时经常遇到的问题.常用的配置汇总到一起. 1.配置jvm参数: 一般在dom ...

  9. python 隐藏命令行窗口_python如何只执行cmd中的动作,但消除或隐藏cmd窗口 - 小众知识...

    [问题] 这里提到的,打包python中,由于python中调用windows的cmd去执行一些动作,所以打包后的python,结果还是会遇到,调用cmd窗口(执行了对应的命令后)一闪而过. 想要消除 ...

最新文章

  1. mysql中没有内置函数_[mysql]MySQL中的内置函数
  2. 联邦学习,为何而生?
  3. PHP Redis 集群封装类
  4. CubeMx 生成的FreeRTOS 代码在ARM compiler6 编译__forceinline 报错的解决方法
  5. java中怎么删除多表连接_在Java中从多个列表中合并和删除重复的最佳方式
  6. MySQL中concat以及group_concat的使用
  7. docker kibana mysql_docker 安装常用组件:[redis,mysql,mongodb,elasticsearch,kibana,exceptionless]...
  8. python 从地址获取数据失败怎么解决_python面试题大全
  9. 计算机管理储存u盘无法使用,小编教你无法格式化u盘怎么解决
  10. angularjs 获取复选框的值_如何利用Python批量获取天眼查企业信息?
  11. IDEA代码文件导航-Navigate使用技巧
  12. android和手环教程,智能手环怎么连接手机_智能手环连接手机教程
  13. lan的以太网标准_并非所有以太网电缆都是平等的:通过升级,您可以获得更快的LAN速度...
  14. 阿里云学生白嫖的服务器有什么用处?
  15. java 打开网页并运行脚本_各种浏览器开启JavaScript脚本方法
  16. matlab 函数 平移,MATLAB图线先下平移
  17. DC/DC电源输入输出滤波电容摆放位置
  18. 你去过(gan)大(huo)年(che),Down机别烦我
  19. 论目前最好的中文搜索引擎
  20. Windows XP 安装 MTP 驱动

热门文章

  1. python控制树莓派风扇_三极管打造树莓派温控风扇
  2. flutter连接不上夜神模拟器问题
  3. 网络编程chapter1
  4. 《考试周刊》杂志正规吗?考试周刊杂志社考试周刊编辑部投稿要求
  5. VScode格式化HTML代码保持标签属性不换行
  6. Zero date value prohibited解决办法
  7. 每架飞机只有一个油箱,一箱油可供一架飞机绕地球飞半圈,空中没有加油机,但飞机之间可以相互加油。...
  8. 当贝美国4K激光投影仪新品Dangbei Mars Pro上手体验
  9. 通过mybatis自定义参数类型转换器,进行数据库字段加密脱敏
  10. 物联网赋予了油液监测新升级