基础语法

一、这部分介绍注释,发出声音,弹窗

(1)简单入门

<1>多行注释
(*
this is multi
comment
*)
<2>发出响声
beep 3

(2)

#表示使用"Daniel"(英国发音)发出声音,人员选择如下图1所示
say "Hello,world" using "Daniel" --或用"--"也可以表示单行注释

              图1
(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                                                                        #获取弹出框输入的内容

        图1

五、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 then
display alert "Var1 is equal to var2"
else if var3 = var4 then
display alert "var3 is equal var4!"
else
display alert "Nothing returned true"
end if

六、for循环

(1)重复固定次数

repeat 3 times
say "This is an action!"
end repeat

(2)

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

八、函数和变量范围

(1)函数示例

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

          图1

(2)

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

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

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

          图1

          图2

          图3

九、

可以通过词典来找相应的方法名称,将应用直接拖到 Dock 上的脚本编辑器图标,然后就会显示扩展的词典(如下图1),在这里可以查看该应用支持的相应方法名称说明,比如Iterm2的词典如下图2所示:

    图1

              图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 listOfFolders
set temp to the name of aFolder
set 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 window
tell window1
set currTab to active tab of window1
set URL of currTab to myBlog
end tell
end tell

(4)ssh快速登录

-- Launch iTerm and log into multiple servers using SSH
tell application "iTerm"
activate
create window with default profile
-- Read serverlist from file path below
set 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 rest
if 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 term
tell current window
create tab with default profile
tell current session
write text "ssh-custom " & nextLine
-- sleep to prevent errors if we spawn too fast
do shell script "/bin/sleep 0.01"
end tell
end tell
end if
end repeat
-- Close the first tab since we do not need it
-- terminate the first session of the current terminal
tell first tab of current window
close
end 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 tab
tell current window
create tab with default profile
end tell -- Prepare horizontal panes
repeat with i from 1 to num_lines
tell session 1 of current tab of current window
if i < num_lines then
split horizontally with default profile
end if
end tell
end repeat -- Prepare vertical panes
set sessid to 1
repeat with i from 1 to num_lines
if i is not 1 then set sessid to sessid + num_cols
if i is not num_lines or num_actions is num_cols * num_lines then
set cols to num_cols - 1
else
set cols to (num_actions - ((num_lines - 1) * num_cols)) - 1
end if
repeat with j from 1 to (cols)
tell session sessid of current tab of current window
split vertically with default profile
end tell
end repeat
end repeat -- Execute actions
repeat with i from 1 to num_actions
tell session i of current tab of current window
set Server to item i of Servers
if length of Server is greater than 0 then
write text "ssh-ele " & Server
do shell script "/bin/sleep 0.01"
end if
end tell
end repeat
end tell

  

Reference:

https://www.youtube.com/watch?v=Nf7sH38RZt4&index=8&list=PLWpwk5XqAMKzsofHtUGgx2wryVCYjc7ya

https://www.bggofurther.com/2017/01/iterm-automatic-mutliple-panes-with-applescript/

022_applescript快速入门教程的更多相关文章

  1. 专为设计师而写的GitHub快速入门教程

    专为设计师而写的GitHub快速入门教程 来源: 伯乐在线 作者:Kevin Li     原文出处: Kevin Li 在互联网行业工作的想必都多多少少听说过GitHub的大名,除了是最大的开源项目 ...

  2. EntityFramework6 快速入门教程

    EntityFramework6 快速入门教程 不得不说EF在国内实在是太小众,相关的技术文章真实屈指可数,而且很多文章都很旧了,里面使用的版本跟如今的EF6差别还是比较大.我刚开始弄这个的时候真是绕 ...

  3. Apple Watch开发快速入门教程

     Apple Watch开发快速入门教程  试读下载地址:http://pan.baidu.com/s/1eQ8JdR0 介绍:苹果为Watch提供全新的开发框架WatchKit.本教程是国内第一本A ...

  4. 指示灯组与3个复位按钮的介绍Arduino Yun快速入门教程

    指示灯组与3个复位按钮的介绍Arduino Yun快速入门教程 1.4.2  指示灯组 指示灯组的放大图如图1.5所示. 图1.5  指示灯组 各个指示灯对应的功能如下: q  RX:对应于0号端口, ...

  5. 游戏控制杆OUYA游戏开发快速入门教程

    游戏控制杆OUYA游戏开发快速入门教程 1.2.2  游戏控制杆 游戏控制杆各个角度的视图,如图1-4所示,它的硬件规格是本文选自OUYA游戏开发快速入门教程大学霸: 图1-4  游戏控制杆各个角度的 ...

  6. Query 快速入门教程

    Query 快速入门教程 http://www.365mini.com/page/jquery-quickstart.htm#what_is_jquery jquery常用方法及使用示例汇总 http ...

  7. Realm for Android快速入门教程

    介绍 如果你关注安卓开发的最新趋势,你可能已经听说过Realm.Realm是一个可以替代SQLite以及ORMlibraries的轻量级数据库. 相比SQLite,Realm更快并且具有很多现代数据库 ...

  8. CMake快速入门教程-实战

    http://www.ibm.com/developerworks/cn/linux/l-cn-cmake/ http://blog.csdn.net/dbzhang800/article/detai ...

  9. .NET Core 快速入门教程

    .NET Core 快速学习.入门系列教程.这个入门系列教程主要跟大家聊聊.NET Core的前世今生,以及Windows.Linux(CentOS.Ubuntu)基础开发环境的搭建.第一个.NET ...

随机推荐

  1. linux常用命令说明

    1. 基础快捷键使用 ctrl + c:停止进程 ctrl + l:清屏 ctrl + r:搜索历史命令 ctrl + q:退出 2.常用目录或文件夹命令 说明:安装linux时,创建一个itcast ...

  2. VS Code +node npm 调试 js

    打开vsCode的调试控制台里面的终端 然后输入下面代码 npm install express-generator -g 1 创建一个命名为 myapp 的应用. express myapp 你就可 ...

  3. java.io.OutputStream & java.io.FileOutputStream

    java.io.OutputStream & java.io.FileOutputStream 1.Java.io.OutputStream(字节输出流) 字节输出流,这是一个抽象类,是表示输 ...

  4. 【演变】Ajax(AjAj)到WebSocket

    提出问题:A  =>  服务器  =>  B           B端浏览器如何知道服务器有A发来的数据? 解决方案: 第1种:频繁轮询    间隔1秒B向服务器讨要数据,就算数据为空.[ ...

  5. python加密(MD5)

    # import hashlib # # 1. 创建一个MD5对象 # obj = hashlib.md5(b"flkjsdalkfjklasdjfklasjkflasdjklfasdjfl ...

  6. webstorm更改scss输出路径

    --no-cache --update $FileName$:$FileParentDir$\css\$FileNameWithoutExtension$.css $FileNameWithoutEx ...

  7. 使用WebView时软键盘遮挡H5页面解决办法

    简单解决办法:在清单文件中添加 android:windowSoftInputMode="adjustResize" 此举可在软键盘弹出时,重新测量布局,保证不遮挡光标的所在位置. ...

  8. You Only Look Once: Unified, Real-Time Object Detection(翻译)

    0 - 摘要 我们提出了YOLO,一种新的物体检测方法.之前的物体检测工作是通过重新使用分类器来进行检测.相反,我们将对象检测抽象为一个回归问题,描述为以空间分隔的边界框和相关的类别概率.一个简单的神 ...

  9. Css - 利于搜索引擎收录的三个标签

    Css - 利于搜索引擎收录的三个标签 <head> <meta charset="utf-8" /> <title>京东(JD.COM)-正品 ...

  10. Aizu 2170 Marked Ancestor

    题意:出一颗树,有两种操作:1. mark  u  标记结点u2.query  u  询问离u最近的且被标记的祖先结点是哪个让你输出所有询问的和. 思路:数据量太小,直接暴力dfs就可以了 #incl ...