I am new to AppleScript - but I have some experience with both PHP and Python. I have been enjoying using AppleScript though, to script in QLab.
But I am stuck now. I’m hoping that someone can point out what I am doing wrong.
The code below is part of a larger project. But I can’t make this part work. My goal is get an array of names and IDs, display a dialog box of the names, allow a user to choose one, and then to do something with the associated ID.
I had hoped that this would do that - but it doesn’t. The “if surfaceName of myItem is mySurface” statement never finds a match and always returns “False”. It should return the surfaceID of the selected surfaceName.
What am I missing?
set mySurfaceList to {{surfaceID:"670133889", surfaceName:"Surface 1"}, {surfaceID:"1436937681", surfaceName:"Surface 2"}, {surfaceID:"33752402", surfaceName:"Surface 3"}}
set mySurfaceNameList to {}
repeat with myItem in mySurfaceList
set end of mySurfaceNameList to surfaceName of myItem
end repeat
set mySurface to choose from list mySurfaceNameList
set mySurfaceID to "None"
repeat with myItem in mySurfaceList
if surfaceName of myItem is mySurface then
set mySurfaceID to surfaceID of myItem
else
set mySurfaceID to "False"
end if
-- display dialog mySurfaceID & (surfaceName of myItem) & mySurface
end repeat
display dialog mySurfaceID
Hi. The result of your choosing from a list is a list item; e.g. {“Surface 2”}. Coerce it to text.
set mySurface to (choose from list mySurfaceNameList) as text
Thanks so much for the quick reply.
I’m afraid, however, that the change that you suggested - assuming that I made it correctly - didn’t solve my problem.
This is the updated version of my script. But it still returns “False”.
set mySurfaceList to {{surfaceID:"670133889", surfaceName:"Surface 1"}, {surfaceID:"1436937681", surfaceName:"Surface 2"}, {surfaceID:"33752402", surfaceName:"Surface 3"}}
set mySurfaceNameList to {}
repeat with myItem in mySurfaceList
set end of mySurfaceNameList to surfaceName of myItem
end repeat
-- set mySurface to choose from list mySurfaceNameList
set mySurface to (choose from list mySurfaceNameList) as text
set mySurfaceID to "None"
repeat with myItem in mySurfaceList
if surfaceName of myItem is mySurface then
set mySurfaceID to surfaceID of myItem
else
set mySurfaceID to "False"
end if
-- display dialog mySurfaceID & (surfaceName of myItem) & mySurface
end repeat
display dialog mySurfaceID
Any other thoughts?
Ah, I see!
In addition to the problem that you found, my if/else code was flawed.
Thank you very much!
Hi,
the most reliable way is to filter the case when the user clicks “Cancel” and add an exit repeat statement.
The else branch is not needed because the repeat loop is always left with a match
set mySurfaceList to {{surfaceID:"670133889", surfaceName:"Surface 1"}, {surfaceID:"1436937681", surfaceName:"Surface 2"}, {surfaceID:"33752402", surfaceName:"Surface 3"}}
set mySurfaceNameList to {}
repeat with myItem in mySurfaceList
set end of mySurfaceNameList to surfaceName of myItem
end repeat
-- set mySurface to choose from list mySurfaceNameList
set mySurface to (choose from list mySurfaceNameList) as text
if mySurface is "false" then return
repeat with myItem in mySurfaceList
if surfaceName of myItem is mySurface then
set mySurfaceID to surfaceID of myItem
exit repeat
end if
end repeat
display dialog mySurfaceID
Thanks for the help. I really appreciate it!
I am having trouble again with something similar.
I am doing a repeat loop, looping through selected cues in QLab, and I’d like to say something like:
set mySelected to (selected as list)
repeat with myCurrentCue in mySelected
if myCurrentCue is the first item of mySelected then
but that never is true. AppleScript never seems to enter the if.
Assuming that this was a issue similar to the one that I struggled with before, I tried variations of
if (myCurrentCue as text) is the (first item of mySelected as text) then
but those just return errors.
Clearly there is something basic that I don’t understand.
What is the proper syntax for checking whether the current item is the first item in a list?
Thank you!
Hi.
You’ve encountered an AppleScript feature which often catches people out.
With the ‘repeat with . in .’ form of repeat, the value of the repeat variable (‘myCurrentCue’ in your case) is a reference to an item in the list, not the value of that item. The reference is automatically resolved to the value in most cases, but not when you’re checking for equality. References can be resolved explicitly with their ‘contents’ property:
repeat with myCurrentCue in mySelected
if myCurrentCue's contents is the first item of mySelected then
But it looks to me as if a numbered repeat would suit your purpose better here.
repeat with i from 1 to (count mySelected)
set myCurrentCue to item i of mySelected -- This IS a value from the list
if i is 1 then
Lovely! Thank you so much for your prompt and helpful answer.
I appreciate it!