Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the '10.7: Finding Control and Menu Items for use in AppleScript User Interface Scripting' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.7: Finding Control and Menu Items for use in AppleScript User Interface Scripting
Authored by: lazybaer on Dec 13, '11 03:53:30AM

is there really no way to get the result in a text file or array by applescript?



[ Reply to This | # ]
10.7: Finding Control and Menu Items for use in AppleScript User Interface Scripting
Authored by: emendelson on Dec 13, '11 06:49:30AM

I asked about this script in Macscripter.net, and the experts there came up with two very useful ways of extending it to create a more manageable list of results:

http://macscripter.net/viewtopic.php?pid=146759



[ Reply to This | # ]
10.7: Finding Control and Menu Items for use in AppleScript User Interface Scripting
Authored by: squalene on Dec 13, '11 07:01:32AM

As far as I can tell, the only way to get it is to write a GUI script that pulls the text out of the Applescript editor's results pane.
I've done that, but the result is UGLY, and not terribly reliable code.
See this thread at MacScriptor: http://macscripter.net/viewtopic.php?id=37674

Obviously the people at Apple took the time to write a very nice parser to generate the text in the first place. It's too bad they never added a getter for it in the editor's script dictionary.



[ Reply to This | # ]
10.7: Finding Control and Menu Items for use in AppleScript User Interface Scripting
Authored by: bretperry on Dec 13, '11 11:04:22AM

You could add code at the end to put winstuff into a text file or an array, not sure how this would be helpful since you are just going to copy it and paste it elsewhere. Just open a text file for writing, set it's contents to winstuff, save and close the file.



[ Reply to This | # ]
10.7: Finding Control and Menu Items for use in AppleScript User Interface Scripting
Authored by: emendelson on Dec 15, '11 07:11:04AM

Nigel Garvey at MacScripter.net provided a script that generates text that can be be pasted into a text editor. In the script below, I added the tell block that pastes text into TextEdit:

property appName : "System Preferences" -- Your app name here.

on listToText(entireContents) -- (Handler specialised for lists of System Events references.)
	try
		|| of entireContents -- Deliberate error.
	on error stuff -- Get the error message
	end try
	
	-- Parse the message.
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"{", "}"} -- Snow Leopard or later.
	set stuff to text from text item 2 to text item -2 of stuff
	set AppleScript's text item delimiters to "\"System Events\", "
	set stuff to stuff's text items
	set AppleScript's text item delimiters to "\"System Events\"" & linefeed
	set stuff to stuff as text
	set AppleScript's text item delimiters to astid
	
	return stuff
end listToText

on main()
	tell application "System Events"
		tell application process appName
			set frontmost to true
			set {windowExists, menuExists} to {front window exists, menu bar 1 exists}
			set {winstuff, menustuff} to {missing value, missing value}
			if (windowExists) then set winstuff to my listToText(entire contents of front window)
			if (menuExists) then set menustuff to my listToText(entire contents of menu bar 1)
		end tell
	end tell
	
	-- the following block is the only part of this code that is NOT by Nigel Garvey
	tell application "TextEdit"
		activate
		make new document at the front
		set the text of the front document to winstuff & return & "-----" & return & menustuff
	end tell
	
	-- 	return {winstuff:winstuff, menustuff:menustuff}
end main

main()


[ Reply to This | # ]
10.7: Finding Control and Menu Items for use in AppleScript User Interface Scripting
Authored by: emendelson on Dec 15, '11 11:10:10AM

And here's a version of Nigel Garvey's script that prompts you to select an application to analyze. I only added the trivial stuff. The intelligent part is by Nigel Garvey.

on run {}
	-- begin lines NOT by Nigel Garvey
	set appAlias to choose file with prompt "Select an application:"
	set appName to name of (info for appAlias)
	set appName to (the reverse of every character in appName) as string
	if ((text 1 thru 4 of appName) as string) is not equal to "ppa." then
		tell me to activate
		display dialog "Please select an application only." buttons {"OK"}
		error number -128
	end if
	set appName to (text 5 thru -1 of appName)
	set appName to (the reverse of every character in appName) as string
	set appPosix to quoted form of POSIX path of appAlias
	do shell script "open " & appPosix
	tell me to activate
	display dialog "Display the window or tab in " & appName & " that you want to analyze and click OK." buttons {"OK", "Cancel"} default button 1
	if button returned of result is "Cancel" then
		error number -128
	end if
	-- end lines NOT by Nigel Garvey
	
	tell application "System Events"
		tell application process appName
			set frontmost to true
			set {windowExists, menuExists} to {front window exists, menu bar 1 exists}
			set {winstuff, menustuff} to {missing value, missing value}
			if (windowExists) then set winstuff to my listToText(entire contents of front window)
			if (menuExists) then set menustuff to my listToText(entire contents of menu bar 1)
		end tell
	end tell
	
	-- the following block is NOT by Nigel Garvey
	tell application "TextEdit"
		activate
		make new document at the front
		set the text of the front document to winstuff & return & "-----" & return & menustuff
	end tell
	-- 	return {winstuff:winstuff, menustuff:menustuff}
end run

on listToText(entireContents) -- (Handler specialised for lists of System Events references.)
	try
		|| of entireContents -- Deliberate error.
	on error stuff -- Get the error message
	end try
	
	-- Parse the message.
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"{", "}"} -- Snow Leopard or later.
	set stuff to text from text item 2 to text item -2 of stuff
	set AppleScript's text item delimiters to "\"System Events\", "
	set stuff to stuff's text items
	set AppleScript's text item delimiters to "\"System Events\"" & linefeed
	set stuff to stuff as text
	set AppleScript's text item delimiters to astid
	
	return stuff
end listToText


[ Reply to This | # ]
10.7: Finding Control and Menu Items for use in AppleScript User Interface Scripting
Authored by: emendelson on Dec 15, '11 12:52:03PM

And there's now an even better version at or near the end of this thread:

http://macscripter.net/viewtopic.php?pid=146849#p146849



[ Reply to This | # ]