AppleScript Quick Start

Source: Internet
Author: User

AppleScript Quick Start

AppleScript, as its name implies, is an apple-developed scripting language that uses AppleScript to manipulate other programs on MacOS systems, click buttons, send messages, simulate automated execution functions, such as opening a browser, emptying the Recycle Bin, and so on. is a very interesting script. Well, to get started quickly, let's start learning about it quickly.

I. Let other programs perform tasks

There is an application called the Script Editor on MacOS, you can search through Launchpad, after opening the Script editor, you can see the support to write and parse the AppleScript and JavaScript two kinds of scripts, as shown in:

AppleScript's grammar is very similar to the usual English grammar, you want the program to do the operation, tell it, for example, you want the Finder to empty the Recycle Bin then write:

tell application "Finder"    empty the trashend tell

Click the Run button on the Script Editor to see that the contents of the Recycle Bin have been emptied, or press the shortcut key Command + R to run, and remember that the Recycle Bin has something to do before running, otherwise it may fail.

If you want to let the system speak, you can write:

tell application "Finder"    say "My name is exchen"end tell

Haha, remember to turn on the sound of the computer, did you hear the words? Not only support English and Chinese, other national languages, such as German, Dutch, the author has tried, also can.

If you want the browser to open the URL, you can write:

set myBlog to "http://www.exchen.net"# 告诉 Chrmoe 浏览器打开 URLtell 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 tellend tell

See if the Chrmoe browser has opened the URL you specified? Funny, huh?

The above test code is run in the Script editor, how to leave the Script Editor, directly on the system to run it? We can save or export the script, click on the File menu and save, you can see the supported format has four kinds,:

Save as a script type, and then execute the script via Osascript, as follows:

/usr/bin/osascript test1.scpt

If you save as an application type, it's a. App package, and you can run it directly by double-clicking it.

Ii. Types of data

The data type of AppleScript is simple, commonly used is number, string, list, record, that is, numeric type, string type, list type, dictionary type.

The assignment and use of numeric types are as follows:

set num1 to 10 # 给 num1 赋值set num2 to 20 # 给 num2 赋值set num3 to num1 + num2 # num1 + num2 赋值给 num3set num4 to num3 * 2 # num3 * 2 赋值给 num4

The assignment and use of string types are as follows:

set str1 to "exchen.net"set str2 to "hehe"set str3 to str1 & str2

The methods for converting strings and numbers are as follows:

set str3Len to the length of str3set numToStr to num1 as stringset strToNum to "123" as number

The list type is actually the equivalent of an array, and the method of defining and manipulating the list type is as follows:

set myLists to {1, 2, "str", 4, 5} # 定义列表数据set item 3 of myLists to "exchen" #操作第三列的数据get myLists  # 获取列表数据

The dictionary type is defined and manipulated as follows:

set myRecord to {name:"exchen", blog:"http://www.exchen.net", body:"hehe"} # 定义 Record 数据set value to the body of myRecord # 从 Record 中获取 body 数据给 valueget value
Third, conditional statements

Since it is a scripting language, of course, the IF and ELSE statements cannot be used as follows:

set num to 123if num = 123 then    display dialog "等于 123"else if strToNum > 456 then    display dialog "大于 456"else    display dialog "不等于 123 也不大于 456"end if

The comparison of strings is judged by the Contains method:

set domainName to "www.exchen.net"if domainName contains "exchen" then    display dialog "包含 exchen"else    display dialog "不包含 exchen"end if
Iv. circulation

There are several ways to write loops, but they are all used repeat ... end repeat, such as looping 100 times can be written like this:

set num to 10repeat 100 times    set num to num + 1end repeatget num

Similar to the For loop, write this:

set num to 5repeat with counter from 0 to num by 1    display dialog counterend repeat

Similar to the while loop, you can write this:

set num to 0repeat until num ≥ 10    display dialog num    set num to num + 3end repeat
Five, function

If some functionality is reusable, it should be written as a function, and AppleScript also supports defining functions, defined and used as follows:

on testFun()    set num to 1end testFuntestFun()

The function will of course have a return value, returning the value by return:

on testFun()    set num to 1    return numend testFunset ret to testFun()get ret

In addition, the function may also take parameters, the method with parameters is as follows:

on testFun(str)    display dialog strend testFuntestFun("exchen")

The function may take more than one parameter, using the following method:

on testFun(str1, str2)    display dialog str1    display dialog str2end testFuntestFun("exchen", "hehe")
Vi. User Interaction dialog box

In front we used the display dialog popup dialog, if you want to specify the title through the WITH title keyword, the code is as follows:

display dialog "这是内容" with title "这是标题"

Specify the contents of the button, can be buttons {"No", "Yes"}, the number of buttons up to three, the code is as follows:

display dialog "这是内容" with title "这是标题" buttons {"No", "Yes"}

You can also set the default button for the selected buttons, with the following code:

display dialog "这是内容" with title "这是标题" buttons {"No", "Yes"} default button "Yes"

You can also specify the icon for the dialog box, the icon can specify the note/stop/caution type, or point to the file path, as follows:

display dialog "这是内容" with title "这是标题" buttons {"No", "Yes"} default button "Yes" with icon note

dialog box is generally used to interact with the user, through the button returned can get the user clicked which button, and then the corresponding operation, the code is as follows:

display dialog "这是内容" with title "这是标题" buttons {"No", "Yes"} default button "Yes"if button returned of result = "Yes" thenelse if button returned of result = "No" thenend if

dialog box can also take the input box, let the user input content, the code is as follows:

display dialog "请输入内容:" default answer ""

The effect of the dialog box with the input box is as follows:

After entering the content, use the text returned to get the contents of the input box:

display dialog "请输入内容:" default answer ""if text returned of result = "exchen" then    get "exchen.net"end if
Vii. Use of dictionaries

In the first section, we know how to perform tasks in other programs, such as letting the browser open the URL, emptying the Recycle Bin, and what if you want to perform any additional features? Where to find the appropriate method name?

You can use the dictionary to find the appropriate method name, drag the app directly to the Script Editor icon on the Dock, and then display the extended dictionary, where you can see a description of the appropriate method names supported by the app, such as the Chrome dictionary as shown:

Some apps do not have a functional extension of the dictionary, you will be prompted to open the dictionary failed, as shown in:

Eight, the interface to operate other programs

In this section, let's try to do other programs to automate, open the calculator, and use entire contents to display the UI information, with the following code:

tell application "System Events"    tell process "Calculator"        entire contents    end tellend tell

Return UI information as follows:

{Window 1 of application process "Calculator" of Application "System Events", Group 1 of Windows 1 of application process " Calculator "of application" System Events ", static text" 0 "of the group 1 of Windows 1 of application Process" Calculator "of Application "System events", Group 2 of window 1 of application process "Calculator" of Application "system events", Butto N 1 of Group 2 of window 1 of application process "Calculator" of Application "System Events", Button 2 of group 2 of Wind OW 1 of application process "Calculator" of Application "System Events", Button 3 of the Group 2 of Windows 1 of application PR Ocess "Calculator" of Application "System Events", Button 4 of the Group 2 of Windows 1 of application Process "Calculator" of Application "System Events", Button 5 of the Group 2 of window 1 of application process "Calculator" of Application "System Ev Ents ", Button 6 of the Group 2 of window 1 of application process" Calculator "of Application" System Events ", Button 7 of Gro Up 2 of Windows 1 of APPLIcation process "Calculator" of Application "System Events", Button 8 of the Group 2 of Windows 1 of application process "CALCU Lator "of application" System Events ", Button 9 of the Group 2 of Windows 1 of application Process" Calculator "of application "System Events", Button ten of the Group 2 of window 1 of application process "Calculator" of Application "system events", butt On one of the Group 2 of the window 1 of application process "Calculator" of Application "System Events", Button of the group 2 of W Indow 1 of application process "Calculator" of Application "System Events", .... column 2 of table 1 of the menu item 1 of Me Nu "Help" of menu bar Item "Help" of the menu bar 1 of application process "Calculator" of Application "System Events", menu item "Calculator Help" of a menu "help" of the menu bar item "Help" of the menu bar 1 of application process "Calculator" of Application "System Events "}

For example, we are concerned about button 9, the information is more, in the moment we do not see the buttons we care about, you can use the tool accessibility Inspector to view the UI information, open the Xcode menu, in the Open Developer tool can find It, open then click on the Capture button to find the button we care about, as shown in the effect:

In the accessibility Inspector interface, you can see that button 9 is the fourth in the second group, as shown in the figure:

The button information can be found in the UI information returned:

button 4 of group 2 of window 1 of application process "Calculator"

Write code to implement the click button:

tell application "System Events"    tell process "Calculator"        entire contents        click button 7 of group 2 of window 1    end tellend tell

If you want to click on the menu, in the UI return message you care about the menu, write code as follows:

tell application "System Events"    tell process "Calculator"        click menu item "关于计算器" of menu "计算器" of menu bar item "计算器" of menu bar 1    end tellend tell

After execution, it is equivalent to clicking on the "About Calculator" menu, as shown in:

Nine, operating parameters

In the first section, we know that the script can be executed through/usr/bin/osascript, what if the script needs parameters when it is started? The parameters are defined by on run and the code is as follows:

on run {parameter1, parameter2}    display dialog parameter1end run

Then at the command line execution, followed by the parameter execution is OK, the command is as follows:

/usr/bin/osascript test1.scpt "exchen.net" "parameter2"

Original address: http://www.exchen.net/applescript-Quick start. html

AppleScript Quick Start

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.