AppleScript快速入门教程

基础语法
AppleScript 入门
一、这部分介绍注释,发出声音,弹窗

(1)简单入门

 <1>多行注释
(*
this is multi
comment
*)
<2>发出响声beep 3
(2)#表示使用"Daniel"(英国发音)发出声音,人员选择如下图1所示say "Hello,world" using "Daniel" --或用"--"也可以表示> 单行注释
图1<br>(3)弹窗
display alert "This is an alert" #弹窗示例

二、变量赋值,string和number类型,字符串连接

set varName3 to true                      #设置布尔值
set varName1 to "This is a string.And I love"   #这是字符串值变量
set varName2 to "12"                      #设置数字变量把双引号去掉即可
set x to varName2 as number                   #转换整形字符串为整形
set y to 2
set z to varName2 * y                     --乘法运算
display dialog z                      --把乘积以弹窗的方式展示,结果为24
--字符串连接展示
set myCountry to " China."
say varName1 & myCountry                  #把连接后的句子读出来

三、列表操作
(1)列表常用操作,获取列表长度,列表元素赋值

set varName to {"A1", "B2", "C3"}
set varName2 to {"D4", "F5", "G6"}
set item 1 of varName to "arun"               #赋值varName列表的"A1"(第1个)为"arun"
set item -3 of varName2 to "arunyang.com"         #赋值varName2列表的"D4"(倒数第3个)为"arunyang.com"
set anotherVarName to varName & varName2
#set anotherVarName to items 2 through 2 of varName2      #items 2 through 2 of 即取值范围(表示从2元素开始,到第2个元素结束),这里为"B2"
set randomValue to some item of anotherVarName        #获取anotherVarName列表里的随机值
set lengthOfList to length of varName             #表示varName列表的长度
say randomValue                       #说出anotherVarName列表里的随机值
say lengthOfList                      #说出varName列表的长度
return anotherVarName
#返回=> {"arun", "B2", "C3", "arunyang.com", "F5", "G6"}

补充:

set myList to {"a", "b", "c", "d", "e", "f"}
set shortList to items 2 through 5 of myList   #返回=>{"b", "c", "d", "e"}(2)列表整形元素合并操作
>set numberVar to 2 as list
set numberVar2 to 4 as list
set numberVar3 to 5 as list
return numberVar & numberVar2 & numberVar3  #合并列表
#返回==>  {2, 4, 5}

(3)列表字符串元素合并操作

set StringVar to "String" as list
set listVar to {"Tacos"}
set StringVar2 to "arun"
return listVar & StringVar & StringVar2   #合并字符串列表
#返回=> {"Tacos", "String", "arun"}

(4)列表之间的合并

set list1 to {1}
set list2 to {2}
set list3 to list1 & list2
set list4 to {"", ""}
set reandom1 to some item of list4
#return list3               #返回=>{1, 2}
return reandom1               #返回=>""

四、获取用户输入
(1)弹窗按钮

set varName to display dialog "Choose an option" buttons {"option1", "option2"}    #如下图1
set varName1 to display dialog "Choose an option" default button "OK" #设置"OK"为默认按钮并高亮,如下图2
set buttonReturned to button returned of varName #返回=>选择的按钮,这里我选"option1"

图1
图2
(2)输入框

set varName to display dialog "Enter some text" default answer "" buttons {"button1", "button2", "button3"} default button "button3"   #弹出输入框,如下图1所示
#set varName1 to display dialog "Enter some text" default answer "Some default Input text"  #设置弹出输入框的默认输入内容
set stringReturned to text returned of varName
get stringReturned      

在这里插入图片描述
五、if条件语句

/=等同于≠

set var1 to 1
set var2 to 2
#if var1 ≠ var2 then    #等于=,不等于/=,小于<,大于>,大于等于>=,小于等于<=
#if var1 is equal to var2 then   #等于=
#if var1 is not equal to var2 then  #不等于
#if var1 is not less than var2 then  #不小于,即大于等于>=
set var3 to 3
set var4 to 4
#if var1 = var2 then #也可以改成or,后面可以接多个and或or语句
if var1 = var2 thendisplay alert "Var1 is equal to var2"
else if var3 = var4 thendisplay alert "var3 is equal var4!"
elsedisplay alert "Nothing returned true"
end if

在这里插入图片描述
六、for循环

(1)重复固定次数

repeat 3 timessay "This is an action!"
end repeat

(2)

set condition to false
repeat until condition is truesay "This is an action"       #触发了一次说的动作,下次condition为true了,所以不会执行了set condition to true         #设置condition为true,这个是结束repeat的条件
end repeat

(3)

set condition to 0
repeat until condition = 3    #condition = 3 是退出条件say "This is an action"    #会重复3次set condition to condition + 1
end repeat
#Result返回3

七、Try and catch

set condition to false
repeat until condition is truetryset age to display dialog "Enter your age" default answer "Age here"set age to text returned of age as numberset condition to true                           #只要输入的是number,这个代码块没有任何error,就会结束循环on error                                     #假如输入的是非number,就会报错,这里捕获错误,beepdisplay alert "You must enter a number"set condition to false                          #设置condition为false就会进入下一个循环,直到condition为trueend try
end repeat
display alert "Everything worked!"

八、函数和变量范围
(1)函数示例

on functionName(param1, param2)set var to param2 + 10display dialog param1 & " " & var
end functionName
functionName("A different string", 43)   #调用函数,如下图1所示

在这里插入图片描述
(2)

<1>函数内的变量为本地变量,函数外的变量为外部变量,两个变量互相隔离,都不能互相引用

<2>要想互相引用需要变成全局变量,即变量前加上global关键字

set var1 to "This is a variable!"         
#var为external variable即外部变量
on function()tryset var to "Inner variable"   #var1为本地变量(local variable)display dialog var           #函数内不能访问外部变量var1,否则会报错"变量没有定义".如图1所示on errorbeepglobal var1            end try
end function
function()
set var to "Potato pie"
display dialog var                  #如图2所示
display dialog var1                 #如图3所示

在这里插入图片描述在这里插入图片描述
在这里插入图片描述
九、可以通过词典来找相应的方法名称,将应用直接拖到 Dock 上的脚本编辑器图标,然后就会显示扩展的词典(如下图1),在这里可以查看该应用支持的相应方法名称说明,比如Iterm2的词典如下图2所示:
在这里插入图片描述
十、使用脚本示例
(1)清空mac回收站

tell application "Finder"         #调用Finder程序empty the trash           #去清空回收站里面的垃圾
end tell                          #结束调用程序

(2)列出所选文件夹中所有的文件夹名称

set folderSelected to choose folder "Select a folder"
tell application "Finder"set listOfFolders to every folder of folderSelected
end tell
set theList to {}
repeat with aFolder in listOfFoldersset temp to the name of aFolderset theList to theList & temp
end repeat

(3)用chrome浏览器打开指定网址

set myBlog to "http://www.arunyang.com"#告诉 Chrmoe 浏览器打开 URL
tell application "Google Chrome"# 新建一个 chrome 窗口set window1 to make new windowtell window1set currTab to active tab of window1set URL of currTab to myBlogend tell
end tell

(4)ssh快速登录

-- Launch iTerm and log into multiple servers using SSH
tell application "iTerm"activatecreate window with default profile-- Read serverlist from file path belowset Servers to paragraphs of (do shell script "/bin/cat /opt/applescript/serverlist")repeat with nextLine in Servers-- If line in file is not empty (blank line) do the restif length of nextLine is greater than 0 then-- set server to "nextLine"-- set term to (current terminal)-- set term to (make new terminal)-- Open a new tab-- tell termtell current windowcreate tab with default profiletell current sessionwrite text "ssh-custom " & nextLine-- sleep to prevent errors if we spawn too fastdo shell script "/bin/sleep 0.01"end tellend tellend ifend repeat-- Close the first tab since we do not need it-- terminate the first session of the current terminaltell first tab of current windowcloseend tell
end tell

(5)多屏登录

#! /usr/bin/osascript
-- List actions to perform
set Servers to paragraphs of (do shell script "/bin/cat /opt/applescript/serverlist")
-- Count number of Servers
--set num_actions to count of actions
set num_actions to count of Servers-- Set cols and lines
set num_cols to round (num_actions ^ 0.5)
set num_lines to round (num_actions / num_cols) rounding up-- Start iTerm
tell application "iTerm"activate# Create new tabtell current windowcreate tab with default profileend tell-- Prepare horizontal panesrepeat with i from 1 to num_linestell session 1 of current tab of current windowif i < num_lines thensplit horizontally with default profileend ifend tellend repeat-- Prepare vertical panesset sessid to 1repeat with i from 1 to num_linesif i is not 1 then set sessid to sessid + num_colsif i is not num_lines or num_actions is num_cols * num_lines thenset cols to num_cols - 1elseset cols to (num_actions - ((num_lines - 1) * num_cols)) - 1end ifrepeat with j from 1 to (cols)tell session sessid of current tab of current windowsplit vertically with default profileend tellend repeatend repeat-- Execute actionsrepeat with i from 1 to num_actionstell session i of current tab of current windowset Server to item i of Serversif length of Server is greater than 0 thenwrite text "ssh-ele " & Serverdo shell script "/bin/sleep 0.01"end ifend tellend repeat
end tell

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407452.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

BASH 中的字符串处理

得到长度%x"abcd" #方法一 %expr length $x 4 # 方法二 %echo ${#x} 4 # 方法三 %expr "$x" : ".*" 4 # expr 的帮助 # STRING : REGEXP anchored pattern match of REGEXP in STRING 复制代码查找子串%expr index $x "b" 2 %expr …

[c++基本语法]——构造函数初始化列表

c构造函数初始化成员变量列表&#xff1a; 1 #pragma once2 class Node3 {4 public:5 int data; // 权值6 Node *parent; // 父节点7 Node *left; // 左子节点8 Node *right; // 右子节点9 public: 10 // 该段代码是c的基本语法中的“…

行业开源应用集锦

06年为开放系统世界写的个封面报道&#xff0c;作者&#xff1a;顾宏军 对开源操作系统、开源数据库&#xff0c;到开源的Web服务器等&#xff0c;基础平台类的开源产品我们已很熟悉。这里&#xff0c;我们将品评从全球数以百万计的开源软件中&#xff0c;挑选出来的CRM、ERP、…

CSS之EM属性

什么是EM&#xff1f; 在一个空白的HTML文档内&#xff0c;你没有任何关于字体大小的声明而只使用默认设置。在大多数浏览器上为html和body标签的默认字体大小为100% 使用EM为单位一定要知道父元素的设置&#xff0c;因为EM是一个相对值&#xff0c;是一个相对于父元素的值 如…

vim多行增加缩进

在Normal Mode下&#xff0c;命令>>将对当前行增加缩进&#xff0c;而命令<<则将对当前行减少缩进。我们可以在命令前使用数字&#xff0c;来指定命令作用的范围。例如以下命令&#xff0c;将减少5行的缩进&#xff1a; 5<< 在Insert/Replace Mode下&#…

PyQt6 QTimer计时器控件

锋哥原创的PyQt6视频教程&#xff1a; 2024版 PyQt6 Python桌面开发 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili2024版 PyQt6 Python桌面开发 视频教程(无废话版) 玩命更新中~共计52条视频&#xff0c;包括&#xff1a;2024版 PyQt6 Python桌面开发 视频教程(无废话版…

前台关于跨域的警告A cookie associated with a cross-site resource at .........,代理服务器

前台关于跨域的警告A cookie associated with a cross-site resource at …&#xff0c;代理服务器 A cookie associated with a cross-site resource at … 解决该警告的方法&#xff1a; 在配置文件中添加配置如下&#xff1a; "proxy": {"/api": {&quo…

按任意的字段旋转的存储过程

---------------------------------------------------------------------------- -- 分段截取函数 ---------------------------------------------------------------------------- CREATE FUNCTION DBO.FUN_SplitStr( S VARCHAR(8000), -- 包含多个数据项的字符串 P…

CSS之REM属性

什么是REM: rem指根em。它的产生是为了帮助人们解决em所带来的计算问题&#xff0c;它是字体排版的一个单位&#xff0c;等同于根font-size。这意味着1rem等同于<html>中的font-size 实例&#xff1a; 正如您看到的&#xff0c;无论您在哪里设置它&#xff0c;1rem的取…

Linux系统下.ko文件是什么文件?.so文件是什么文件?

.so 文件是动态链接库文件&#xff0c;相当于 win下的 .dll 文件。 .ko 是内核模块文件&#xff0c;是内核加载的某个模块&#xff0c;一般是驱动程序。

vue-cli3全面配置详解

vue-cli3全面配置详解 vue-cli3-config 创建项目 配置环境变量 通过在package.json里的scripts配置项中添加–mode xxx来选择不同环境 在项目根目录中新建.env, .env.production, .env.analyz等文件 只有以 VUE_APP_ 开头的变量会被 webpack.DefinePlugin 静态嵌入到客户端侧…

Android 开发环境在 Windows7 下的部署安装

Android SDK Android SDK 为 Android 应用的开发、测试和调试提了必要的API库和开发工具。 ADT Bundle 下载 如果你是一个android 开发新手&#xff0c;推荐你下载使用 ADT Bundle 以快速开始android 的开发&#xff0c;它提供了必要的 android sdk 组件和一个内置 ADT 的 Ecli…

CSS之REM和EM的区别

事实证明。rem 和 em 均有各自的优缺点。应给根据实际情况来判断其使用方式 1.如果这个属性是根据它的font-size进行测量&#xff0c;则该属性最好使用em 2.其他的一切事物均使用rem #

Cisco路由器故障诊断技术(3)

Cisco路由器故障诊断技术(3)<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />3.4 trace命令 trace命令提供路由器到目的地址的每一跳的信息。它通过控制IP报文的生存期&#xff08;TTL&#xff09;字段来实现。TTL等于1的ICM…

《疯狂的简洁》书摘

头脑和常识催生简洁 复杂会让人带来安全感&#xff0c;复杂和简洁是一对矛盾&#xff0c;复杂不一定优于简洁 复杂可以变得很丑陋 一切始于简洁&#xff0c;坚持原则&#xff0c;抵御复杂&#xff0c;头脑聪慧且真心实意 人们喜欢事情的透明化 直率即简洁&#xff0c;迂回就是复…

linux kill 关闭进程命令

点评&#xff1a;杀死进程最安全的方法是单纯使用kill命令&#xff0c;不加修饰符&#xff0c;不带标志。 首先使用ps -ef命令确定要杀死进程的PID&#xff0c;然后输入以下命令&#xff1a; # kill -pid 注释&#xff1a;标准的kill命令通常都能达到目的。终止有问题的进…