AppleScript快速入门教程
生活随笔
收集整理的這篇文章主要介紹了
AppleScript快速入门教程
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
基礎(chǔ)語法
AppleScript 入門
一、這部分介紹注釋,發(fā)出聲音,彈窗
(1)簡單入門
<1>多行注釋 (* this is multi comment *) <2>發(fā)出響聲beep 3 (2)#表示使用"Daniel"(英國發(fā)音)發(fā)出聲音,人員選擇如下圖1所示say "Hello,world" using "Daniel" --或用"--"也可以表示> 單行注釋 圖1<br>(3)彈窗 display alert "This is an alert" #彈窗示例二、變量賦值,string和number類型,字符串連接
set varName3 to true #設(shè)置布爾值 set varName1 to "This is a string.And I love" #這是字符串值變量 set varName2 to "12" #設(shè)置數(shù)字變量把雙引號去掉即可 set x to varName2 as number #轉(zhuǎn)換整形字符串為整形 set y to 2 set z to varName2 * y --乘法運(yùn)算 display dialog z --把乘積以彈窗的方式展示,結(jié)果為24 --字符串連接展示 set myCountry to " China." say varName1 & myCountry #把連接后的句子讀出來三、列表操作
(1)列表常用操作,獲取列表長度,列表元素賦值
補(bǔ)充:
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)彈窗按鈕
(2)輸入框
五、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循環(huán)
(1)重復(fù)固定次數(shù)
repeat 3 timessay "This is an action!" end repeat(2)
set condition to false repeat until condition is truesay "This is an action" #觸發(fā)了一次說的動作,下次condition為true了,所以不會執(zhí)行了set condition to true #設(shè)置condition為true,這個是結(jié)束repeat的條件 end repeat(3)
set condition to 0 repeat until condition = 3 #condition = 3 是退出條件say "This is an action" #會重復(fù)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,就會結(jié)束循環(huán)on error #假如輸入的是非number,就會報(bào)錯,這里捕獲錯誤,beepdisplay alert "You must enter a number"set condition to false #設(shè)置condition為false就會進(jìn)入下一個循環(huán),直到condition為trueend try end repeat display alert "Everything worked!"八、函數(shù)和變量范圍
(1)函數(shù)示例
(2)
<1>函數(shù)內(nèi)的變量為本地變量,函數(shù)外的變量為外部變量,兩個變量互相隔離,都不能互相引用
<2>要想互相引用需要變成全局變量,即變量前加上global關(guān)鍵字
set var1 to "This is a variable!" #var為external variable即外部變量 on function()tryset var to "Inner variable" #var1為本地變量(local variable)display dialog var #函數(shù)內(nèi)不能訪問外部變量var1,否則會報(bào)錯"變量沒有定義".如圖1所示on errorbeepglobal var1 end try end function function() set var to "Potato pie" display dialog var #如圖2所示 display dialog var1 #如圖3所示
九、可以通過詞典來找相應(yīng)的方法名稱,將應(yīng)用直接拖到 Dock 上的腳本編輯器圖標(biāo),然后就會顯示擴(kuò)展的詞典(如下圖1),在這里可以查看該應(yīng)用支持的相應(yīng)方法名稱說明,比如Iterm2的詞典如下圖2所示:
十、使用腳本示例
(1)清空mac回收站
(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瀏覽器打開指定網(wǎng)址
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總結(jié)
以上是生活随笔為你收集整理的AppleScript快速入门教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 熊猫烧香部分源码
- 下一篇: rsync同步时,删除目标目录比源目录多