Macro: Edit Linked or In-Line AppleScript in Script Debugger/Editor

Macro: Edit Linked or In-Line AppleScript

This is a pretty useful macro for those who are diving in and out of Script Debugger or Script Editor and Keyboard Maestro.

It is used to either

  • Extract the text of a text script action and create a new script document in your script editor with the extracted text, then compile it to continue editing in the editor
  • Open the linked script file in your script editor (similar to option clicking the folder icon next to the file path, but instead of opening the script file in Finder, it will open it in your editor).

It's nice because the trigger is the same either way, as you can see if the gif below.

Shout out to @ccstone for the bulk of the plist extraction.

NOTE
I use Script Debugger by default so if you don't have it, you just need to adjust the line that says:
set scriptApp to "Script Debugger" to set scriptApp to "Script Editor"

Here's the script all together if interested:

        (*
===============================================================================
          		Edit Linked or In-Line AppleScript
===============================================================================

Version: 1.0												Updated: 02/17/2018 06:14 CST
By: Kevin Funderburg

PURPOSE:

When you trigger this script, it will determine if the action copied is an
in-line or linked AppleScript and open the result in your preferred AppleScript
editor (default is Script Debugger).
- If it is in-line, it will compile the text in your editor


- Shout out to Chris Stone for a lot of the plist set up


REQUIRED:
	1.	Mac OS X Yosemite 10.10.5+
	2.	Mac Applications
			• Script Debugger or Script Editor
				
	3.	EXTERNAL OSAX Additions/LIBRARIES/FUNCTIONS
			• None
					
	4.	INTERNAL FUNCTIONS:
			• SearchandReplace()


VERSION HISTORY:
1.0 - Initial version.
===============================================================================
*)
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
---------------------------------------------------------------------------------

set kmPlistStr to missing value
set scriptPath to missing value

if (the clipboard) starts with "<?xml" then
	try
		set kmPlistStr to the clipboard as text
		
		tell application "System Events"
			set actionPlistRecord to value of (make property list item with properties {text:kmPlistStr})
			set UseText to |UseText| of item 1 of actionPlistRecord
			
			if UseText is true then
				set theText to |Text| of item 1 of actionPlistRecord
				
				set scriptApp to "Script Debugger" -- Change to desired script editor (Script Debugger/Script Editor)
				
				if scriptApp is "Script Editor" then
					tell application "Script Editor"
						set newScript to make new document with properties {text:theText}
						tell newScript
							compile
							activate
						end tell
					end tell
				else if scriptApp is "Script Debugger" then
					tell application "Script Debugger"
						set newScript to make new document with properties {source text:theText, debugger enabled:true}
						tell newScript
							compile
							activate
						end tell
					end tell
				end if
				
			else
				set scriptPath to |path| of item 1 of actionPlistRecord
				if scriptPath starts with "~" then
					set scriptPath to my SearchandReplace(scriptPath, "~", (POSIX path of (path to home folder)) as text)
				end if
				
				set scriptPath to scriptPath as POSIX file
				
				if scriptPath ≠ "" then
					tell application "Finder"
						open scriptPath
					end tell
				else
					beep
				end if
			end if
		end tell
		
	on error
		beep
	end try
	
else
	display notification ¬
		"Clipboard not XML" with title ¬
		"Edit Linked or in-Line AppleScript" subtitle ¬
		"Error:" sound name "Basso"
end if

---------------------------------------------------------------------------------
on SearchandReplace(sourceText, replaceThis, withThat)
	set theString to current application's NSString's stringWithString:sourceText
	set theString to theString's stringByReplacingOccurrencesOfString:replaceThis withString:withThat
	return theString as text
end SearchandReplace

09)Edit Linked or In-Line AppleScript.kmmacros (6.6 KB)

5 Likes

This is opening both SE and SD for me on two systems, and neither comes to the front as in your GIF. Please advise? Also, why does your AppleScript formatting look different than mine? :sweat_smile:

Hmm, what kind of action is selected (i.e. Execute Text Script or Execute Script File) when you execute the macro? Does it happen for both kinds?
Also, do you have "Default editor for OSA scripts, applets and droplets" checked at the bottom of the Preferences > General pane of SD? Because if SE is still your default the script may be opening the the script in SD, but the OS is saying to open it in SE. Could be wrong though.

I just adjusted the fonts within SE and SD to be SF Mono (Apple standard coding font for now) and then adjusted the colors and format (like bold and italic) to my liking (looks like picture below).

1 Like

It happens with both; however, I realize now that SD does come to the front. I was just being impatient.

Yes I do, and SD is set as the default editor in SE's preferences too.

Thank you!

Hey Kevin,

Your macro looks pretty good!   :sunglasses:

One little issue that jumps out at me:

if (the clipboard) starts with "<?xml" then
   try
      set kmPlistStr to the clipboard as text

You’ve wasted a trip here by getting the clipboard twice. While that’s not a big deal, those little things can add up.

Keep in mind too that with Keyboard Maestro 8.x you can get the xml of a selected action directly.

tell application "Keyboard Maestro"
   
   set selectedActionList to selection
   
   if selectedActionList ≠ {} then
      
      set selectedAction to item 1 of selectedActionList
      
      if class of selectedAction = action then
         set actionXML to xml of selectedAction
      else
         error "The selected item is NOT an action!"
      end if
      
   end if
   
end tell

-Chris

Thanks for the feedback on both counts! I’ll make the adjustments pronto!

Hey Folks,

Don't you just love it when you need a macro, you know it's on the forum, but you can't find it...

I was really wanting Kevin's macro yesterday, but I just couldn't find it for whatever reason.

So I made my own:

Edit Script of Selected Action in Active Macro v1.00.kmmacros (11 KB)

And then of course I found Kevin's macro right after I finished it.  :blush:

My macro is not especially superior to Kevin's, but since it uses several different techniques than his I'm posting it for people who would like to see those differences.

Unlike Kevin's macro mine only works with Script Debugger, although that wouldn't be difficult to change. (I might change it to work with Apple's Script Editor if SD is not on the user's system soon. I didn't last night, because I completely ran out of gas.)


By the way – I recommend that anyone who writes any AppleScript at all should download and use the latest version of Script Debugger.

The trial period is 20 days, but at the end of the trial Script Debugger will revert from Pro mode to Lite mode – and Lite mode is still far superior to Apple's Script Editor for writing AppleScript.

The Pro version costs $99.99 (U.S.), but the Lite version is completely free.

If you write AppleScript then be sure to see what you've been missing all these years.

-Chris


Note: I've used Script Debugger continuously since Mark Alldritt released version 1.0 in 1996, and I've never benefitted financially in any way from evangelizing it.


1 Like

I agree wholeheartedly. When I first starting learning AppleScript via Script Editor, the process was slow and rather frustrating. After I got the trial version of SD I knew within an hour that this was the way to learn AppleScript, totally worth the investment!

1 Like

Interesting. I do love Script Debugger in principle, and recognise the ineptness of Script Editor. But I can’t pull myself out of using the latter most of the time for finding the former far too busy to look at. It has so much capability that it works against me, I find.

I keep SD on hand for when I need to debug a complex script. Sometimes SE won’t even tell you what line the error is coming from.

But my other favourite all round editor is Atom. Clean, simple, beautiful, functional. But it executes AppleScripts via osascript, which isn’t ideal, but satisfactory for every quickies.

1 Like

Perhaps you haven't learned how to control the SD View. It can be very, very clean, like this:

You can toggle the Results/Inspector panel from the menu. I've set a shortcut for to make it easy:

image

And, of course, you can fully customize the toolbar:

image

So, you can start out simple and clean, and then with the touch of a key get great detail.

1 Like

Hey CJ,

As JM has demonstrated SD can be paired down to very little UI.

As for myself – I'd go crazy if I didn't have at least the code-pane, the result-pane, and the variables-pane showing – along with the break-point gutter.

Like JM I have keyboard shortcuts set up to show/hide most UI elements, so I can alter this setup at will almost instantly.

-Chris

2 Likes

That was just for demo purposes -- I never use SD in that config.

Exactly.