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. 详略。。设计模式1——单例。。。。studying
  2. python tkinter button颜色变不了_更改函数中Tkinter按钮的颜色
  3. 算法--三种方法求连续子数组的最大和
  4. wxWidgets:wxDirTraverser类用法
  5. zookeeper中展示所有节点_Zookeeper数据结构与监听机制
  6. HTTP 错误 404.2 - Not Found 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面 详细错误:HTTP 错误...
  7. 初识Xen-CentOS5.8_x64位系统安装xen文档(-)
  8. Json学习总结(2)——Java 下的 JSON库性能比较:JSON.simple vs. GSON vs. Jackson vs. JSONP
  9. Ancient Berland Circus CodeForces - 1C
  10. 目标检测java系统_5分钟!用Java实现目标检测
  11. Ubuntu开启nfs并连接开发板
  12. Linux运维之道(大量经典案例、问题分析,运维案头书,红帽推荐)
  13. (附源码)基于PHP在线考试系统的设计与实现 毕业设计 032028
  14. 微信java版s40_微信诺基亚下载
  15. 光照度和光强度的区别
  16. QComboBox选项置灰、不可选择、文本颜色(汉字颜色)
  17. 资源收藏:扁平化风格的图标
  18. java中字段可以取名is开头吗
  19. Java百分比格式化
  20. 似然函数Likelihood function

热门文章

  1. mybatis 去重方法 之distinct
  2. 学生DW静态网页设计——代码质量好-家乡广州 (5页) HTML+CSS+JavaScript web网页制作期末大作业
  3. gin框架学习三之gorm
  4. 旋转矩阵更容易理解与记忆的推导,从2D到3D
  5. python实现注册登录系统_python实现登录与注册系统
  6. 十分钟入门Zigbee
  7. Google Protocol Buffer 的使用和原理(c++)
  8. NormalSpaceSampling 基于法向空间的点云降采样
  9. java 面包屑导航_基于SpringBoot打造在线教育系统(7)-- 面包屑导航与子分类
  10. Android Studio 源码移植到系统中