aTbRef: improvements to AppleScript articles

I finally got around to adding some pointers, arising from forum discussions, into the aTbRef section on Applescript. Thanks to all forum member cotributing their expertise and, in particular, thanks to @sumnerg .

5 Likes

Drawing on some helpful feedback, I’ve expanded the AppleScript a bit more and broken some of the larger articles into smaller ones allowing for better linking.

Question/corrections welcome. I think the four articles on objects’ properties could probably do with a simple explanatory code example to indicate the syntax. Code suggestions welcomed!

The TBX zip, search index and sitemap are all updated.

Side note; I’ve adjusted the site CSS so the grey background to <code> elements is a bit lighter to give better contrast balance from the white page background and to the dark red text.

1 Like

What I’ve tried to dig into with the code examples is the fine detail of the code syntax. AppleScript’s app dictionary UI lacks such info so having to guess the code to write is a bore. I suspect that missing info also nerfs general occasional use of AppleScript except to run others’ completed scripts.

An interesting example, realised from this re-write, AppleScript doesn’t let you describe a note by its $Path. Instead you have to ā€˜find’ (find note in operator) the note at the path and make a variable of that object and then use the latter. You can write it as a one liner, but the line gets very long and hard to read. Also there is no copy/duplicate method, though one can communicate with the app’s menu using general AppleScript menus. I also noted the app’s AS dictionary has no notion of aliases (so, making an alias would have to use menu interaction). Perhaps I need to add an article with a non-exhaustive list of unsupported note management tasks?

That might include:

  • Making aliases. A workaround is to make an agent to find only the to-be-aliased note, move the child alias to a desired place then deleting the agent. Existing aliases are addressed as if actual notes, being aware that an alias/original state can be checked by reading its $IsAlias value.
  • Copying notes/agents/adornments. Here, I think using AppleScript System Events to drive the menus is the workaround.
  • Adding ā€˜built-in’ features (as from app file menu). Again, use AppleScript System Events†.
  • making/deleting links. The app’s dictionary is mainly for dealing with existing links. Currently this task has to be effected via an ā€˜evaluate’ call (akin to action code eval) on action code operators—i.e. the expression being evaluated as in the code example here.

The point of the above list is not to militate for loads more built-in AppleScript dictionary functionality (as the ROI for that isn’t clear) but to indicate, for those making occasional try-out of AppleScript use, that a solution is possible even if not via a single baked-in code. Using System Events makes very verbose code that is new-user unfriendly, thus the suggestion in the footnote.

†. Perhaps some AS-competent person would like to write a pair of AS sub-routines (aka functions) for calling menu items or menus sub-menu items akin to those seen in this answer #2 of this StackOverlow thread. If such a solution emerges, I’d happily add that to the aTbRef AppleScript section. As the solution to a number of ā€˜missing’ tasks is to drive menus, having a re-useable subroutine where you just feed in the current arguments (e.g. menu option label) makes a lot of sense.

1 Like

A generic ā€œhandlerā€ to call a menu item could be something like this:

-- call example 1: my do_menu("Tinderbox 10", "Stamps", "Turn red")
-- call example 2: my do_menu("Tinderbox 10", "Edit", "Duplicate")

on do_menu(app_name, menu_name, menu_item)
	try
		tell application app_name to activate --must bring to front		
		tell application "System Events"
			tell process app_name
				tell menu bar 1
					tell menu bar item menu_name
						tell menu menu_name
							click menu item menu_item
						end tell
					end tell
				end tell
			end tell
		end tell
		return true
	on error error_message
		return false
	end try
end do_menu

This seems to work for sub-menu items:

-- call example: my do_menu("Tinderbox 10", "Format", "Font", "Bold")

on do_menu(app_name, menu_name, menu_item, sub_item)
	try
		tell application app_name to activate --must bring to front		
		tell application "System Events"
			tell process app_name
				tell menu bar 1
					tell menu bar item menu_name
						tell menu menu_name
							tell menu item menu_item
								tell menu menu_item
									click menu item sub_item
								end tell
							end tell
						end tell
					end tell
				end tell
			end tell
		end tell
		return true
	on error error_message
		return false
	end try
end do_menu
1 Like

Thanks. I’ve added a new aTbRef article: Accessing app menu items.

Site map, search index and TBX file zip are updated online.