You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

A few questions about AppleScript (that I’m annoyed about)

I have a couple of questions below about the programming language AppleScript.

The first question: Why is my AppleScript not working? (When answering, please put "[The number of the question goes here] [Answer goes here.]" I am trying to learn AppleScript, and I have tried making the following script recently, but it shows an error.

[AppleScript]
set theApp to (choose from list {"Safari", "Chrome", "Scripts", "App store", "Mail", "Facetime", "Messages", "Notes"} with prompt "Choose an app to open" with title "Some applications to open")
tell application theApp
	activate
	display dialog "hello"
end tell
[AppleScript]

And here is the error:

It is very annoying, and I want to know what's wrong.

The second question: What does "ignoring" mean in AppleScript?

The third and final question: I have looked at AppleScript's examples, and I would like to know why this code WORKS.

try
	--insert some actions under here
	display dialog "hello"
on error the error_message number the error_number
	set the error_text to "Error: " & the error_number & ". " & the error_message
	display dialog the error_text buttons {"OK"} default button 1
	return the error_text
end try

Assuming the user clicks "Cancel," it will display the error "Error: -128. User canceled." It seems it is just making variables out of nowhere according to how it compiles. And it still manages to work without saying  " /"error_message/" is not defined.



iMac 21.5″, macOS 10.15

Posted on Dec 12, 2020 1:34 PM

Reply
Question marked as Top-ranking reply

Posted on Dec 13, 2020 10:58 AM

> Why is my AppleScript not working


In your first script you use choose from list.


choose from list always returns a list as its result. Even if there's only one item selected, it's still a list.

Therefore, if the user selects, say, Safari, the result is:


{"Safari"}


i.e., a list containing a single string.


This is not a valid parameter to the tell application command. tell application requires a string, not a list.


The simple solution is to extract the string from the list, via something like:


set theApp to (choose from list {"Safari", "Chrome", "Scripts", "App store", "Mail", "Facetime", "Messages", "Notes"} with prompt "Choose an app to open" with title "Some applications to open")
set theAppName to item 1 of theApp
tell application theAppName
	-- code goes here
end tell


Note how I use a new variable theAppName to extract the first (and only) item from the list.

Similar questions

5 replies
Question marked as Top-ranking reply

Dec 13, 2020 10:58 AM in response to Zurcrescred556

> Why is my AppleScript not working


In your first script you use choose from list.


choose from list always returns a list as its result. Even if there's only one item selected, it's still a list.

Therefore, if the user selects, say, Safari, the result is:


{"Safari"}


i.e., a list containing a single string.


This is not a valid parameter to the tell application command. tell application requires a string, not a list.


The simple solution is to extract the string from the list, via something like:


set theApp to (choose from list {"Safari", "Chrome", "Scripts", "App store", "Mail", "Facetime", "Messages", "Notes"} with prompt "Choose an app to open" with title "Some applications to open")
set theAppName to item 1 of theApp
tell application theAppName
	-- code goes here
end tell


Note how I use a new variable theAppName to extract the first (and only) item from the list.

Dec 12, 2020 1:39 PM in response to Zurcrescred556

DaBoyy wrote:

I have a couple of questions below about the programming language AppleScript.
The first question: Why is my AppleScript not working? (When answering, please put "[The number of the question goes here] [Answer goes here.]"

No matter what you do, do NOT literally reply

[The number of the question goes here] [Answer goes here.]


Dec 13, 2020 11:06 AM in response to Zurcrescred556

> The second question: What does "ignoring" mean in AppleScript?


AppleScript has a number of rules it follows when running. ignoring... overrides those. It has a corollary of considering.


For example, text comparison is normally case-insensitive, but you can control this via the ignoring|considering case statement:


ignoring case
	"a" = "A" --> true
end ignoring

considering case
	"a" = "A" -- false
end considering


Other things you can ignore/consider include:


application responses - normally when AppleScript sends a command to an application, it waits for the result. If you don't care about this (i.e. you want the command to execute in the background while your script moves onto other steps), you can use ignoring application responses.


punctuation - Normally the string "Hello, world." and "hello world" would not match, but ignoring punctuation ignores the commas, periods, etc. so they do match


There are others, too.

Dec 13, 2020 11:15 AM in response to Zurcrescred556

> Assuming the user clicks "Cancel," it will display the error "Error: -128. User canceled." It seems it is just making variables out of nowhere according to how it compiles


For this, you have to understand what try/end try does.


When any command fails it returns two variables, the error message and the error number. These can be used by the developer to determine what action to take (ignore, bail, alert the user, etc.).

Normally when this happens, AppleScript itself posts the error message in a dialog. However, when using a try/end try statement, these variables are exposed to your script so you can take whatever action you like. In this case, the line:


> on error the error_message number the error_number


catches the error and puts the error message into the variable error_message, and puts the error number into the variable error_number.


In the case of display dialog, clicking the 'Cancel' button automatically triggers an error -128 with the message 'User Cancelled', and that's what's passed into the on error handler.

A few questions about AppleScript (that I’m annoyed about)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.