Applescript Studio Plugins How To
-1) I’m not a writer, nor a professional coder… it’s in point form and the code may blow up… you have been warned. (give me cudos, not law suits)
- Plugins: What (resources that add functionality and are seperately loaded and developed… as in any extra application related resources… data, functions, nib files… ANYTHING), Why (more developers, better seperation of parts)
.5) Ingredients: property list osax from late nite software, xcode developer tools (probably works with earlier versions and project builder; but I haven’t tried it and the steps may change)
- Getting your project ready… (or you could make a new one…)
– Edit application target: Project > Add New Build Phase > Copy Files > Plug-Ins
– In MainMenu.nib add a “Submenu” to the main menu
– – set the title and remove unwanted items
– – give the /menu item/ an awake from nib handler in a new script ( see (A) )
– – – by menu item I mean: The menu item (as seen in the property inspector) /on/ the menu bar
– – – awake from nib
– – – – detects all the plugins
– – – – loads their property list
– – – – makes a menu item based on key in property list
– – – – attaches a script to the menu item
– – – – – has the plugin’s path
– – – – – has the plugin’s plist record
– – – – – and has a handler to run the plugin’s script
– Group > New Group > “PlugIn Resources”
– – This is for holding all the stuff for plugins so it doesn’t mix with the rest of your project
- Creating PlugIns
– Project > Add New Target > Legacy > Bundle (don’t add to Application target… we’ll do that after)
– Group > New Group > PlugIn Resources : PlugIn1
– Project > Add New File… > Applescript > Applescript File (add to plugin1 target, not to application)
– – put in group we made for it…
on run argv
return "plugin one checking in"
end run
– Edit the Target’s Info.plist in Expert View
– – New Sibling > PlugInMenuTitle > String > PlugIn One
– – New Sibling > PlugInEntryPoint > String > plugin1.applescript
– Edit Application’s Target…
– – drag from Project:Products:PlugIn1.bundle
– – drop to Targets:Project:Copy Files Build Phase(PlugIns):
- Other Thoughts about plugins
– It may be possible to have a plugin load a nib with a panel and display it modally to the applications main window…
– – (note that the plugin script is in it’s own applescript context so you would need to pass a reference to the window in the args)
– (note that the plugin script is in it’s own applescript context so it is a little resource heavy to build and tear down that context on each run)
– you may even want to have perl script plugins, or data only plugins… differentiate in the bundle’s plist with a PlugInType key or something
– USE THE PLIST LUKE
– – You can put all kind of data in here and it’s easy to edit and access in your script
– Check out a plugin based app’s info panel in the Finder!!! coolness
– for others to develop publish what variables you send to the run handler and the kind of thing you expect in a result, and the expected plist keys
A) The Code – plug in menu’s script
on make_menu_script(an_item, a_pList, a_path)
script PlugInMenuItemScript
property _path : a_path
property _pList : a_pList
on choose menu item an_item
set the_script_path to (_path & ":Contents:Resources:" & _pList's |PlugInEntryPoint|)
set the_message to run script file the_script_path
-- with parameters arg_list
log the_message as string
-- do something with the result
end choose menu item
end script
set an_item's script to PlugInMenuItemScript
end make_menu_script
on awake from nib theObject
try
set plugins_folder to POSIX file ((main bundle's path as string) & "/Contents/PlugIns/")
repeat with each_plugin in list folder plugins_folder
set plugin_path to ((plugins_folder as string) & each_plugin)
set a_pList to read property list file (plugin_path & ":Contents:info.plist")
set plugin_title to a_pList's |PlugInMenuTitle|
set a_menu_item to make new menu item at the end of the menu items of the sub menu of theObject with properties {title:plugin_title, enabled:true}
make_menu_script(a_menu_item, a_pList, plugin_path)
end repeat
on error message
log message
end try
end awake from nib
B) development issues
– make sure you clean all targets between builds… some changes may not be applied with multiple targets
– when making a plugin as a seperate xcode project start with an empty project and add the bundle target as before in (2)
– – (don’t use one of the built in bundles–if you are desperate make your own template ( that’s for another time ) )
C) Inspiration Credit: Joe Zobkiw “Mac OS X – Advanced Development Techniques”, Cocoa/Carbon Plugin chapters