### Return Statement Example (with value) Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_handlers.html Example of a 'return' statement that exits a handler and returns a specific integer value. ```applescript return 2 -- returns integer value 2 ``` -------------------------------- ### EPPC Specifier Examples with Hostname Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html Examples of valid eppc-style specifiers using hostnames. These demonstrate connecting with and without providing user credentials. ```applescript "eppc://myCoolMac.local" -- hostname, no user or pwd ``` ```applescript "eppc://myUserName:pwd@myCoolMac.local" -- user, pwd, and hostname ``` -------------------------------- ### Get Command for Object Property Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_lexical_conventions.html Uses the `get` command to retrieve a property of an object. This example gets the name of the front window of the Finder application. ```applescript get name of front window of application "Finder" ``` -------------------------------- ### Filter Finder Windows by Name Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_reference_forms.html This example shows how to specify all open Finder windows that do not have the name "AppleScript Language Guide". It uses the 'every' and 'whose' clauses to filter the results. ```applescript tell application "Finder" every window whose name is not "AppleScript Language Guide" end tell ``` -------------------------------- ### Folder Actions Setup Application Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_folder_actions.html The Folder Actions Setup application allows management of Folder Actions, including enabling/disabling actions, viewing associated scripts, and adding/removing folders and scripts. ```APIDOC ## Folder Actions Setup Application ### Description Manages Folder Actions in macOS. ### Location `/System/Library/CoreServices` ### Features - Enable or disable Folder Actions. - View folders with associated scripts. - View and edit scripts associated with a folder. - Add or remove folders from the list. - Associate one or more scripts with a folder. - Enable or disable all scripts for a folder. - Enable or disable individual scripts for a folder. - Remove scripts associated with a folder. ``` -------------------------------- ### Combined Application Usage Example Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_control_statements.html An example showing how to combine terms from different applications (Mail and Safari) in a single script, sending different commands to each. ```applescript search the web for the sender of the first item of ¬ (get selected messages of the front message viewer) ``` -------------------------------- ### Get Application Version Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html An example of an absolute object specifier to get the version of an application. ```applescript version of application "Finder" --result: "10.5.1" ``` -------------------------------- ### Return Statement Example (without value) Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_handlers.html Example of a 'return' statement used without an expression, which exits the handler immediately without returning any value. ```applescript return -- no value returned ``` -------------------------------- ### Example: `with transaction` with a Session Object Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_control_statements.html This example shows how to specify a session object for a `with transaction` statement, allowing for more granular control over the transaction context. This requires the target application to support session parameters. ```applescript tell application "Super DB" ``` ```applescript set mySession to make session with data {user: "Bob", password: "Secret"} ``` ```applescript with transaction mySession ``` ```applescript ... ``` ```applescript end transaction ``` ```applescript end tell ``` -------------------------------- ### EPPC Specifier Examples with IP Address Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html Examples of valid eppc-style specifiers using IP addresses. These show how to connect with or without specifying user authentication details. ```applescript "eppc://123.23.23.123" -- IP address, no user or pwd ``` ```applescript "eppc://myUserName:pwd@123.23.23.123" -- user, pwd, and IP address ``` ```applescript "eppc://myUserName@server.company.com" -- server address, user ``` -------------------------------- ### Example Log Output Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html This shows the expected output in the Event Log when the logging script is executed. ```applescript (*Where*) ``` ```applescript (*is*) ``` ```applescript (*the*) ``` ```applescript (*hammer*) ``` -------------------------------- ### Specify a Folder by Name Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html This example demonstrates how to specify a folder named 'Applications' within the startup disk using the 'of' keyword to define the container. ```applescript folder "Applications" of startup disk ``` -------------------------------- ### Execute a Command from Imported Terminology Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_control_statements.html This example demonstrates using a command ('search the web for') that is assumed to be imported from a resource like Safari. ```applescript search the web for "AppleScript" ``` -------------------------------- ### system info Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Gets information about the system. ```APIDOC ## system info ### Description Gets information about the system. ### Syntax `system info` | __ | required ``` -------------------------------- ### Example: Modifying a Record with `with transaction` Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_control_statements.html This example demonstrates using `with transaction` to ensure a database record is modified exclusively. It obtains current field values, prompts the user for new values, and updates the fields within a transaction. ```applescript tell application "Small DB" ``` ```applescript with transaction ``` ```applescript set oldName to Field "Name" ``` ```applescript set oldAddress to Field "Address" ``` ```applescript set newName to display dialog ¬ "Please type a new name" ¬ default answer oldName ``` ```applescript set newAddress to display dialog ¬ "Please type the new address" ¬ default answer oldAddress ``` ```applescript set Field "Name" to newName ``` ```applescript set Field "Address" to newAddress ``` ```applescript end transaction ``` ```applescript end tell ``` -------------------------------- ### Continue Statement Example Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_handlers.html Example demonstrating how to override an AppleScript command with a local handler and use 'continue' to pass control to the original command. This handler displays a dialog before potentially beeping. ```applescript on beep numTimes set x to display dialog "Start beeping?" buttons {"Yes", "No"} if button returned of x is "Yes" then ¬ continue beep numTimes -- Let AppleScript handle the beep. -- In this example, nothing to do after returning from the continue. end beep ``` ```applescript beep 3 --result: local beep handler invoked; shows dialog before beeping ``` ```applescript tell my parent to beep 3 -- result: AppleScript beep command invoked ``` -------------------------------- ### Execute Script Stored on Disk Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html This example shows how to execute a script that is stored on disk using an alias. ```applescript set scriptAlias to "Leopard:Users:myUser:Documents:savedScript.scptd:" as alias ``` ```applescript run script scriptAlias --result: script is executed ``` -------------------------------- ### Start Timing Operations Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html Record the current time to measure the duration of subsequent operations. ```applescript set t to (time of (current date)) --Start timing operations ``` -------------------------------- ### Basic Script Application Example Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_about_handlers.html A simple script that can be saved as a script application. When run, it closes the frontmost Finder window and then quits. ```applescript tell application "Finder" close front window end tell ``` -------------------------------- ### Arithmetic Operator Precedence Example Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_operators.html Demonstrates operator precedence where multiplication is performed before addition. ```applescript 12 + 2 * 5 --result: 22 ``` -------------------------------- ### Round Nearest .5 Example Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Shows how 'to nearest' rounding handles .5 cases by rounding to the nearest even integer. ```applescript round 0.5 --result: 0 ``` -------------------------------- ### Round Down Example Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Demonstrates rounding a positive number down to the nearest integer. ```applescript round 1.1 rounding down --result: 1 ``` -------------------------------- ### Initialize an Empty List Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html Declare an empty list variable. This is a common starting point for building lists. ```applescript set bigList to {} ``` -------------------------------- ### Choose from List Example Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Selects a person from Address Book and displays their birthday. Handles user cancellation and ensures the result is treated as a list. ```AppleScript tell application "Address Book" ``` ```AppleScript set bDayList to name of every person whose birth date is not missing value ``` ```AppleScript choose from list bDayList with prompt "Whose birthday would you like?" ``` ```AppleScript if the result is not false then ``` ```AppleScript set aName to item 1 of the result ``` ```AppleScript set theBirthday to birth date of person named aName ``` ```AppleScript display dialog aName & "'s birthday is " & date string of theBirthday ``` ```AppleScript end if ``` ```AppleScript end tell ``` -------------------------------- ### Create Finder Startup Disk Reference Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_operators.html Creates a reference object pointing to the Finder's startup disk. ```AppleScript tell app "Finder" to set diskRef to a ref to startup disk ``` -------------------------------- ### Get All Properties of Finder Window in AppleScript Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_reference_forms.html Fetch a list of all properties and their values for the first window of the Finder application. This is useful for introspection. ```applescript tell app "Finder" properties of window 1 end tell ``` -------------------------------- ### Getting the Class of Boolean Literal in AppleScript Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html This example demonstrates how to get the class of the Boolean literal 'true', which returns 'boolean'. ```applescript class of true --result: boolean ``` -------------------------------- ### Define and Run a Simple Script Object Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html This example demonstrates how to define a script object named 'helloScript' that displays a dialog, and then how to invoke it using the 'run' command. ```applescript script helloScript display dialog "Hello." end script ``` ```applescript run helloScript -- invoke the script ``` -------------------------------- ### Getting the Class of a Type in AppleScript Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html You can query the class of a type using the 'class of' syntax. These examples show the class of 'text' and 'integer' types. ```applescript class of text --result: class ``` ```applescript class of integer --result: class ``` -------------------------------- ### Get Downloads Folder Path Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Retrieves the alias path to the user's Downloads folder. This is a common starting point for file operations. ```applescript set downloadsFolder to path to downloads folder ``` -------------------------------- ### Getting the Class of Various Types in AppleScript Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html These examples show how to retrieve the class of different data types, including text, and a list containing mixed types. ```applescript class of "Some text" --result: text ``` ```applescript class of {1, 2, "hello"} --result: list ``` -------------------------------- ### Specify a Property Using Possessive Form Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html This example shows how to get the name of the first window in TextEdit using the possessive form ('s) to specify the container and its property. ```applescript tell application "TextEdit" first window's name end tell ``` -------------------------------- ### AppleScript Index Syntax Examples Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_reference_forms.html Demonstrates various ways to specify an object's position within a container using index notation. This includes integer, ordinal, and keyword-based indexing. ```applescript item 1 of the startup disk ``` ```applescript item index 1 of the startup disk -- "index" is usually omitted ``` ```applescript the first item of the startup disk ``` ```applescript word 2 of paragraph 3 ``` ```applescript 2nd word of paragraph 3 ``` ```applescript second word of paragraph 3 ``` ```applescript word –1 of paragraph 3 ``` ```applescript last word of paragraph 3 ``` ```applescript word –2 of paragraph 3 ``` ```applescript -2th word of paragraph 3 ``` -------------------------------- ### Get Path to Resource in Finder Bundle Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Retrieves the path to a resource file within a specified application bundle. The example targets a '.icns' file within the Finder application. ```applescript tell application "Finder" set gearIconPath to path to resource "Gear.icns" end ``` -------------------------------- ### Specify Range of Words in AppleScript Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_reference_forms.html Define a range of words within a list using start and stop indexes. This example shows an invalid range that would result in an error. ```applescript words 1 thru 3 of {1, 2, 3} ``` -------------------------------- ### Specify Items in Finder Folder (Plural Syntax) Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_reference_forms.html This script demonstrates an alternative syntax using the plural class name ('items') to specify all items within the 'Users' folder on the startup disk. It requires the Finder application to be running. ```applescript tell application "Finder" items of folder "Users" of startup disk end tell ``` -------------------------------- ### Choose File Name with POSIX Path Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html Prompt the user to choose a file name, starting the dialog in a specific POSIX directory like '/tmp'. ```applescript set fileName to choose file name default location (POSIX file "/tmp") ``` -------------------------------- ### Get ASCII Code from Character in AppleScript Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Returns the integer code associated with a specified character. Note: This command is deprecated starting in AppleScript 2.0; use the `id` property of the `text` class instead. The result depends on user language preferences. ```applescript set codeValue to ASCII number "¬" --result: 194 ``` -------------------------------- ### Declare a list variable with the set command Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_variables.html Use the 'set' command to declare a list variable. This example creates a list containing numbers and a string. ```applescript set myList to { 1, 2, "four" } --result: {1, 2, "four"} ``` -------------------------------- ### Get Character from ASCII Code in AppleScript Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Returns the character corresponding to a specified integer code. Note: This command is deprecated starting in AppleScript 2.0; use the `id` property of the `text` class instead. The encoding used depends on user language preferences. ```applescript set theChar to ASCII character 65 --result: "A" ``` ```applescript set theChar to ASCII character 194 --result: "¬" ``` ```applescript set theChar to ASCII character 2040 --result: invalid range error ``` -------------------------------- ### Invalid AppleScript Identifiers Examples Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_lexical_conventions.html Examples of invalid identifiers in AppleScript. ```applescript C- back&forth 999 Why^Not ``` -------------------------------- ### Variable Assignment with 'set' Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Demonstrates how to use the 'set' command to create new variables, assign new values to existing variables, and modify properties of objects like lists. ```APIDOC ## SET ### Description The `set` command is used to assign values to variables, create new variables, and modify properties or elements of objects. ### Method `set` ### Endpoint N/A (AppleScript command) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```applescript -- Create a new variable set myWeight to 125 -- Assign a new value to an existing variable set myWeight to myWeight + 23 -- Change an element in a list set intList to {1, 2, 3} set item 3 of intList to 42 -- Change a property of an application object tell application "Finder" to set name of startup disk to "Happy Fun Ball" ``` ### Response #### Success Response (200) None (modifies variables or objects in place) #### Response Example None ``` -------------------------------- ### Valid AppleScript Identifiers Examples Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_lexical_conventions.html Examples of valid identifiers in AppleScript. ```applescript areaOfCircle Agent007 axis_of_rotation ``` -------------------------------- ### Get Path to Desktop Folder Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Returns the path to the desktop folder. Specify 'as string' to get the path as text. ```applescript path to desktop ``` ```applescript path to desktop as string ``` -------------------------------- ### Open a URL with the appropriate program Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Use the `open location` command to open a specified URL with its default associated application. The `error reporting` parameter is historical and no longer supported. ```applescript open location "http://www.apple.com" ``` -------------------------------- ### Create a Reference to the First Document Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html Creates a reference object pointing to the first document of an application. ```applescript tell application "TextEdit" set docRef to a reference to the first document --result: document 1 of application "TextEdit" -- an object specifier name of docRef --result: "New Report.rtf" -- name of the specified object end tell ``` -------------------------------- ### Import Multiple Applications for Combined Terminology Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_control_statements.html Demonstrates importing multiple applications to access terms from different sources simultaneously, which is not possible with 'tell' blocks alone. ```applescript use application "Mail" ``` ```applescript use application "Safari" ``` -------------------------------- ### Getting the Current Date in AppleScript Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html Use the 'current date' command to get the system's current date and time. The result is a 'date' object. ```applescript set theDate to current date ``` ```applescript --result: "Friday, November 9, 2007 11:35:50 AM" ``` -------------------------------- ### Run Application Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Executes a specified application. The application must be on a local or mounted volume. ```applescript run application "TextEdit" ``` -------------------------------- ### Define a List Using Expressions Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html Create a list where items are defined using expressions, demonstrating dynamic list creation. ```applescript { "it" & "'s", 1 + 1, 4 > 3 } ``` -------------------------------- ### Choose Application Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Allows the user to choose an application from a dialog. ```APIDOC ## CHOOSE APPLICATION /api/choose_application ### Description Allows the user to choose an application from a dialog. ### Method CHOOSE APPLICATION ### Endpoint /api/choose_application ### Parameters #### Query Parameters - **with title** (text) - optional - Title text for the dialog. Default Value: "Choose Application" - **with prompt** (text) - optional - A prompt to be displayed in the dialog. Default Value: "Select an application:" - **multiple selections allowed** (boolean) - optional - Allow multiple items to be selected? If `true`, the results will be returned in a list, even if there is exactly one item. Default Value: `false` - **as** (class) - optional - Specifies the desired class of the result. If specified, the value must be one of `application` or `alias`. Default Value: `application` ### Request Example ```applescript choose application with prompt "Choose a web browser:" choose application with multiple selections allowed choose application as alias ``` ### Response #### Success Response (200) - **application or alias** (application | alias) - The selected application, as either an `application` or `alias` object. If multiple selections are allowed, returns a list containing one item for each selected application, if any. ### Error Handling Signals a “user canceled” error if the user cancels the dialog. ### Discussion The `choose application` dialog initially presents a list of all applications registered with the system. To choose an application not in that list, use the Browse button, which allows the user to choose an application anywhere in the file system. ``` -------------------------------- ### Getting the Current Application Object Specifier in AppleScript Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html Shows how to get the object specifier for the `current application` constant. This represents the script's execution environment. ```applescript get current application --result: current application ``` -------------------------------- ### Get Unicode code point for a character Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html Use the 'id' property to get the Unicode code point of a single character. This replaces older 'ASCII number' commands. ```applescript id of "A" ``` -------------------------------- ### Basic Integer Examples in AppleScript Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html Illustrates direct integer values and simple arithmetic operations. AppleScript handles integers within a specific range, converting larger numbers to reals. ```applescript 1 ``` ```applescript set myResult to 3 - 2 ``` ```applescript -1 ``` ```applescript 1000 ``` -------------------------------- ### Get Middle Object in AppleScript Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_reference_forms.html Use the 'middle' reference form to get the middle item from a list or collection. This works for lists with an odd or even number of elements. ```applescript tell application "TextEdit" end tell ``` ```applescript middle paragraph of front document ``` ```applescript middle item of {1, "doughnut", 33} ``` ```applescript middle item of {1, "doughnut", 22, 33} ``` ```applescript middle item of {1, "doughnut", 11, 22, 33} ``` -------------------------------- ### Accessing Application and AppleScript Versions in Finder Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html Demonstrates how to retrieve the version of the target application ('Finder') and the AppleScript version itself within a 'tell' block. ```applescript tell application "Finder" ``` ```applescript version --result: "10.5.1" ``` ```applescript version of AppleScript --result: "2.0" ``` ```applescript end tell ``` -------------------------------- ### Display a basic alert Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Use `display alert` to show a simple warning message to the user. This example demonstrates the basic syntax for displaying an alert with a message. ```applescript set alertResult to display alert "Insert generic warning here." ``` -------------------------------- ### Get File End-of-File Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Use 'get eof' to retrieve the length of a file in bytes. The parameter can be an alias, file specifier, or an integer file descriptor obtained from 'open for access'. -------------------------------- ### Index Reference Form Example Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_reference_forms.html An example of an object specifier using index reference forms to identify an object by its number within a container. This form requires specifying the container. ```applescript item 1 of second folder of disk 1 ``` -------------------------------- ### Get Environment Variable with AppleScript Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Retrieves the value of a specified shell environment variable. If the variable is not defined, an empty string is returned. This command can also be used to get Gestalt values. ```applescript system attribute "SHELL" --result: "/bin/bash" (for example) ``` -------------------------------- ### choose folder Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Enables the user to select a directory (folder or disk). This command supports various options for customizing the dialog, such as a custom prompt, default starting location, and allowing multiple selections. ```APIDOC ## choose folder ### Description Allows the user to choose a directory, such as a folder or a disk. ### Syntax `choose folder` `choose folder with prompt` _text_ `choose folder default location` _alias_ `choose folder invisibles` _boolean_ `choose folder multiple selections allowed` _boolean_ `choose folder showing package contents` _boolean_ ### Parameters - **with prompt** (text) - Optional - The prompt to be displayed in the dialog. Default: No prompt is displayed. - **default location** (alias) - Optional - The folder to begin browsing in. Default: Last selected location or user's Documents folder. - **invisibles** (boolean) - Optional - Show invisible folders? Default: `false`. - **multiple selections allowed** (boolean) - Optional - Allow multiple items to be selected? If `true`, results are returned as a list. Default: `false`. - **showing package contents** (boolean) - Optional - Show the contents of packages? Default: `false`. ### Result The selected directory, as an `alias`. If multiple selections are allowed, returns a list of `alias` objects. Signals a “user canceled” error if the user cancels the dialog. ### Examples ```applescript set foldersList to choose folder with prompt "Select as many folders as you like:" with multiple selections allowed ``` ```applescript set folderName to quoted form of POSIX path of (choose folder) ``` ``` -------------------------------- ### choose URL Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Prompts the user to specify a URL, either by typing it or selecting from available services. Returns the URL as text. ```APIDOC ## choose URL ### Description Allows the user to specify a URL. The user can either type a URL directly or select from a list of discovered services (e.g., web servers, FTP servers). ### Method `choose URL` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters #### `showing` _list_ (of service types or _text_) (optional) A list specifying the types of services to show. Can include predefined types like `"Web servers"`, `"FTP Servers"`, etc., or Bonjour service types (e.g., `"_ftp._tcp"`). Default is `File servers`. #### `editable URL` _boolean_ (optional) Determines if the URL text field is editable. If `true` (default), the user can type a URL. If `false`, the user can only select from the provided list. ### Request Example ```applescript set myURL to choose URL tell application "Finder" to open location myURL ``` ### Response #### Success Response (200) - **result** (text) - The URL specified by the user. #### Response Example ```json { "result": "http://www.example.com" } ``` #### Error Handling - Signals a “user canceled” error if the user cancels the dialog. - The command does not verify if the entered text is a valid URL. ``` -------------------------------- ### Repeat With Loop (Range) in AppleScript Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_control_statements.html The 'repeat with n from 2 to x' syntax iterates from a start value to a stop value, incrementing by a step value (defaulting to 1). Values for start, stop, and step are evaluated once at the beginning of the loop. ```applescript on factorial(x) set returnVal to 1 repeat with n from 2 to x set returnVal to returnVal * n end repeat return returnVal end factorial ``` -------------------------------- ### get eof Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Returns the length of a file in bytes. ```APIDOC ## get eof ### Description Returns the length of a file, in bytes. ### Method `get eof` ### Syntax `get eof` | _fileSpecifier_ ### Parameters #### _fileSpecifier_ The file to obtain the length for, as an alias, a file specifier, or an `integer` file descriptor. A file descriptor must be obtained as the result of an earlier `open for access` call. ### Result The logical size of the file, that is, the length of its contents in bytes. ``` -------------------------------- ### Specify Every Word in a Text String (Plural Syntax) Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_reference_forms.html This example demonstrates an alternative syntax using the plural class name ('words') to specify all words within a given text string. The result is a list of the words. ```applescript set myText to "That's all, folks" words of myText ``` -------------------------------- ### Clipboard Operations Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Interacts with the system clipboard to get or set data. ```APIDOC ## The Clipboard ### Description Returns the contents of the clipboard or sets data to the clipboard. ### Syntax `the clipboard` `set the clipboard to [anything]` ### Parameters - **as** (class): Optional. The type of data desired. `the clipboard` will attempt to find that “flavor” of data on the clipboard; if it is not found, it will attempt to coerce whatever flavor is there. ### Result The data from the clipboard, which can be of any type. ### Request Example (Get Clipboard) ```applescript get the clipboard ``` ### Request Example (Set Clipboard) ```applescript set the clipboard to "Some text to put on the clipboard." ``` ### Discussion It is not necessary to use the clipboard to move data between scriptable applications. You can simply `get` the data from the first application into a variable and `set` the appropriate data in the second application. ``` -------------------------------- ### Equivalent Record Expressions in AppleScript Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html Shows how AppleScript evaluates expressions within a record before using the record. This example demonstrates that records with calculated property values are equivalent to those with pre-calculated values. ```applescript { name:"Steve", height:76 - 1.5, weight:150 + 20 } ``` ```applescript { name:"Steve", height:74.5, weight:170 } ``` -------------------------------- ### Round Down Negative Example Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Demonstrates rounding a negative number down to the nearest integer. ```applescript round -1.1 rounding down --result: -2 ``` -------------------------------- ### Run Script with Parameters Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Executes a script and passes a list of parameters to its 'run' handler. ```applescript run script myScriptFile with parameters {parameter1, parameter2} ``` -------------------------------- ### Another Integer Literal Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html Another example of a valid integer literal expression for a number. ```applescript 2 ``` -------------------------------- ### Open For Access Command Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Opens a specified file for reading and optionally writing, returning a file descriptor for subsequent file operations. ```APIDOC ## open for access ### Description Opens a file for reading and writing. ### Method `open for access` ### Parameters #### Path Parameters - **fileSpecifier** (_alias_ | _file_) - Required - An alias or file specifier for the file to open. #### Query Parameters - **write permission** (boolean) - Optional - Specifies if writing to the file should be allowed. Defaults to `false`. ### Request Example ```applescript set theFile to (path to desktop as text) & "NewFile" set referenceNumber to open for access theFile -- To open with write access: -- set referenceNumber to open for access theFile with write permission ``` ### Response #### Success Response (200) - **integer** - A file descriptor that can be used with other file commands (`read`, `write`, `get eof`, `set eof`, `close access`). #### Response Example ```applescript -- referenceNumber will be an integer representing the opened file. ``` ``` -------------------------------- ### launch Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Launches an application if it is not already running. It does not send a 'run' command and has no effect if the application is already running. This is useful for opening an application without performing its usual startup procedures. ```APIDOC ## launch ### Description Launches an application, if it is not already running, but does not send it a `run` command. If an application is already running, sending it a `launch` command has no effect. ### Method `launch` ### Endpoint N/A (This is a command, not an API endpoint) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```applescript launch application "TextEdit" ``` ### Response #### Success Response (200) None. The command performs an action. #### Response Example None ``` -------------------------------- ### End Timing Operation Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html Calculate the elapsed time since the start of timing operations. ```applescript set total to (time of (current date)) - t --End timing ``` -------------------------------- ### Activate Application Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Brings an application to the front, launching it if necessary. It can take an application object as a parameter. ```APIDOC ## ACTIVATE /api/activate ### Description Brings an application to the front, launching it if necessary. ### Method ACTIVATE ### Endpoint /api/activate ### Parameters #### Path Parameters - **application** (application) - required - The application to activate. ### Request Example ```applescript activate application "TextEdit" ``` ### Response #### Success Response (200) None. ### Discussion The `activate` command does not launch applications on remote machines. For examples of other ways to specify an application, see the `application` class and Remote Applications. ``` -------------------------------- ### Get HFS Path from Alias Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html Coerce an alias object to text to retrieve its HFS path. ```applescript notesAlias as text --result: "Hard_Disk:Users:myUser:Feb_Notes.rtf" ``` -------------------------------- ### Specify Every Word in a Text String Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_reference_forms.html This example demonstrates how to use the 'every' reference form to specify all words within a given text string. The result is a list of the words. ```applescript set myText to "That's all, folks" every word of myText --result: {"That's", "all", "folks"} (a list of three words) ``` -------------------------------- ### Get Text from Document Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html Obtain a text object containing all text extracted from a specific document. ```applescript set docText to text of document "MyFavoriteFish.rtf" of application "TextEdit" ``` -------------------------------- ### File Access Operations Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Explains how to open files for read/write access, manage file descriptors and pointers, and the advantages of explicit file handling. ```APIDOC ## File Access Commands ### Description AppleScript provides commands for explicit file access, allowing read and optionally write operations on file contents. This is distinct from opening a file in an application. ### Commands - `open for access` file - `close access` file descriptor - `read` file descriptor [from integer] [for integer] [until text_item] - `write` data [to file descriptor] [starting at integer] - `get eof` file descriptor - `set eof` file descriptor to integer ### File Descriptor Calling `open for access` returns an integer file descriptor, representing an open communication channel to the file's data. This descriptor must be closed using `close access`. ### File Pointer Each file descriptor maintains a file pointer, marking the current position within the file. `read` and `write` operations typically start at the file pointer and advance it. ### Advantages of Explicit Access - **Performance**: Reduces overhead when performing multiple operations on the same file. - **Sequential Operations**: Simplifies reading or writing data sequentially due to the file pointer. ### Considerations - Opening a file multiple times results in multiple file descriptors, each needing separate closure. - Concurrent writes to the same file descriptor are not recommended and may lead to unspecified behavior. ``` -------------------------------- ### Get the Class of a List Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html Determine the class of a list literal. This snippet demonstrates the 'class of' command. ```applescript class of {"this", "is", "a", "list"} --result: list ``` -------------------------------- ### Get POSIX Path of Alias Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html Retrieves the POSIX-style path for an alias object. This property is read-only. ```applescript POSIX path of zApp --result: "/System/Library/CoreServices/Finder.app/" ``` -------------------------------- ### choose file Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Presents a file picker dialog to the user, allowing them to select one or more files. ```APIDOC ## choose file ### Description Allows the user to choose a file. ### Method Not applicable (this is a command, not an HTTP endpoint). ### Endpoint Not applicable. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```applescript set aFile to choose file with prompt "HTML or RTF:" of type {"public.html", "public.rtf"} invisibles false set picturesFolder to path to pictures folder choose file of type "public.image" with prompt "Choose an image:" default location picturesFolder invisibles false ``` ### Response #### Success Response (200) The selected file, as an `alias`. If multiple selections are allowed, returns a list containing one `alias` for each selected file, if any. #### Response Example ```json { "selected_file": "Macintosh HD:Users:username:Documents:example.html" } ``` ### Error Handling Signals a “user canceled” error if the user cancels the dialog. ``` -------------------------------- ### choose from list Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Presents the user with a list of items to choose from. This command is highly customizable with options for titles, prompts, default selections, button names, and allowing multiple or empty selections. ```APIDOC ## choose from list ### Description Allows the user to choose items from a list. ### Syntax `choose from list` _list_ `choose from list with title` _text_ `choose from list with prompt` _text_ `choose from list default items` _list_ `choose from list OK button name` _text_ `choose from list cancel button name` _text_ `choose from list multiple selections allowed` _boolean_ `choose from list empty selection allowed` _boolean_ ### Parameters - **list** (list of number or text) - Required - A list of items for the user to choose from. - **with title** (text) - Optional - Title text for the dialog. Default: No title is displayed. - **with prompt** (text) - Optional - The prompt to be displayed in the dialog. Default: "Please make your selection:". - **default items** (list of number or text) - Optional - A list of items to be initially selected. Default: No items are selected. - **OK button name** (text) - Optional - The name of the OK button. Default: "OK". - **cancel button name** (text) - Optional - The name of the Cancel button. Default: "Cancel". - **multiple selections allowed** (boolean) - Optional - Allow multiple items to be selected? Default: `false`. - **empty selection allowed** (boolean) - Optional - Allow the user to choose OK with no items selected? Default: `false`. ``` -------------------------------- ### Extract Characters as List Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html Use the `characters` element to get individual characters within a range as a list. ```applescript get characters 3 thru 7 of "Try this at home" ``` ```applescript --result: {"y", " ", "t", "h", "i"} ``` -------------------------------- ### Path To Application Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Retrieves the file system location of a specified application. ```APIDOC ## path to (application) ### Description Returns the file system path to a specified application. ### Method `path to` ### Endpoint N/A (This is a command, not an HTTP endpoint) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **application** (application specifier) - Optional - The application to locate. Defaults to `it`. - Possible values include `current application`, `frontmost application`, `me`, `it`, or an application name. - **as** (class: `alias` | `text`) - Optional - The class of the returned location. Defaults to `alias`. ### Request Example ```applescript path to application "TextEdit" -- Result: alias "Leopard:Applications:TextEdit.app:" path to me -- Result: alias "Leopard:Applications:AppleScript:Script Editor.app:" path to current application -- Result: alias "Leopard:Applications:AppleScript:Script Editor.app:" ``` ### Response #### Success Response (200) - **Result** (alias | text) - The location of the specified application. ``` -------------------------------- ### Running Applications and Scripts in AppleScript Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html Details the 'run' command for executing applications and script handlers. ```APIDOC ## Running Applications and Scripts in AppleScript ### Description The `run` command executes the `run` handler of a specified target, typically an application or a script object. It launches applications hidden by default. ### Syntax `run` _runTarget_ ### Parameters - **`runTarget`** (script or application) - The script or application object to run. _Default Value:_ `it` (the current target) ### Result The result, if any, returned by the specified object’s `run` handler. ### Examples ```applescript run application "TextEdit" tell application "TextEdit" to run run myScript -- where myScript is a script object ``` ### Discussion Applications can be specified by name or by a more precise file path. AppleScript automatically launches an application if it's not already running when a command is sent to it. Use `launch` for starting an application without its usual startup procedures. ``` ```APIDOC ## Running a Script with Parameters ### Description This command runs a specified script or script file, optionally passing parameters to it and specifying the scripting component. ### Syntax `run script` _scriptTextOrFileSpecifier_ `with parameters` _listOfParameters_ `in` _text_ ### Parameters - **`scriptTextOrFileSpecifier`** (text | alias | file) - Required. The script text, or an alias or file specifier for the script file. - **`listOfParameters`** (list of anything) - Optional. A list of parameter values to pass to the script. - **`text`** (text) - Optional. The scripting component to use. _Default Value for `text`:_ `"AppleScript"` ### Result The result of the script’s `run` handler. ``` -------------------------------- ### Relative Object Specifier Example Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html Demonstrates a relative object specifier that requires context to be fully resolved. ```applescript name of item 1 of disk 2 ``` -------------------------------- ### Call an AppleScript Handler with No Parameters Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_about_handlers.html Invokes the 'helloWorld' handler, which takes no arguments. ```applescript helloWorld() -- Call the handler ``` -------------------------------- ### Get POSIX Path from Alias Source: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html Use the 'POSIX path' property to obtain a POSIX-style path from an alias object. ```applescript POSIX path of notesAlias --result: "/Feb_Notes.rtf" ```