Read text in menu item to save as a variable

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:

e70b58abb05b012c70d53e22416922f00d237d6a_2_690x431

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

Offtopic: I’d like to understand why you use (?s). If the s is optional then I thought the regex must be (s?).

2 Likes

Me too. My bad, just a typo. Funny enough the code works anyway.

1 Like

Does the Index number of the items change or can you just choose the item by its index and not have to do regex?