Can I read the text in a particular menu item and then save it as a variable?
For example in Logic Pro, depending on how many regions/tracks are selected, some export options change as you can see below:
So I would like to run the script, it would read both of those items’ text at that particular time and save each as a separate variable
As the numeric values can be 1 or more and Track/Region can be singular or plural a reasonable approach is a search with Regular Expression.
The script puts all names of the Export menu into a list and extracts the desired names
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
activate application "Logic Pro X"
tell application "System Events"
tell process "Logic Pro X"
tell menu "Export" of menu item "Export" of menu "File" of menu bar 1
set allNames to name of menu items
end tell
end tell
end tell
set regionString to nameOfMenuItemByRegex(allNames, "\\d+ Regions? as Audio")
set TrackString to nameOfMenuItemByRegex(allNames, "\\d+ Tracks? as Audio")
on nameOfMenuItemByRegex(nameList, regex)
repeat with aName in nameList
if contents of aName is not missing value then
set nameString to my (NSString's stringWithString:aName)
set theRange to (nameString's rangeOfString:regex options:(current application's NSRegularExpressionSearch))
if theRange's |length|() is not 0 then return nameString as text
end if
end repeat
return missing value
end nameOfMenuItemByRegex