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

10.6: Create new AppleScript document Service System 10.6
Snow Leopard only hintI got tired of copy-pasting AppleScripts I found online into the AppleScript Editor, so I made a simple Automator Service to do it for me.

Just launch Automator, choose new service (that receives text), add the Run AppleScript action and replace the script body with this:
on run {input, parameters}
  
  tell application "AppleScript Editor"
    activate
    if contents of document 1 is "" then
      set contents of document 1 to (input as string)
    else
      make new document with properties {contents:(input as string)}
    end if
  end tell
  
  return input
end run
Save it as 'New AppleScript Document with selection' or whatever name you prefer.

Activate it under System Preferences » Keyboard » Keyboard Shortcuts » Services » Text » "New AppleScript Document with selection" if it does not show up in the contextual menu when you have selected some text.

[crarko adds: I tested this, and it works as described. If you have the Developer Tools installed it has a Service called Development » Make New AppleScript that does the same thing. It is disable by default so you'd need to turn it on using the Keyboard System Pref as described above. This hint provides a customizable alternative.]
    •    
  • Currently 4.40 / 5
  You rated: 5 / 5 (5 votes cast)
 
[6,986 views]  

10.6: Create new AppleScript document Service | 10 comments | Create New Account
Click here to return to the '10.6: Create new AppleScript document Service' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Create new AppleScript document Service
Authored by: matt_strange on Mar 24, '11 09:26:56AM

To make this work with Script Debugger, change the 3 instances of the contents keyword to script source.

Also, on my 10.6 machine, the Developer service is simply called "Make New AppleScript". Not sure why that is different.

Now, my question is: where are the workflows that are part of the developer set stored? I'd like to change them to use Script Debugger, but I can't locate them.



[ Reply to This | # ]
10.6: Create new AppleScript document Service
Authored by: NaOH-Lye on Mar 24, '11 09:54:36AM

I'm not certain the editor's note about the Developer Tools is correct. I've never installed them, but the option in System Preferences > Keyboard > Keyboard Shortcuts for Make New AppleScript is available to me in 10.6.



[ Reply to This | # ]
Re: 10.6: Create new AppleScript document Service
Authored by: Uncle Asad on Mar 24, '11 01:54:52PM

Yeah, on 10.5, Script Editor declares the Services itself (in its Info.plist), so unless something's changed massively with AppleScript Editor on 10.6, the Service is provided by the app, not developer tools. (I've used that Service since at least 10.3.)



[ Reply to This | # ]
Re: 10.6: Create new AppleScript document Service
Authored by: xhinter on Mar 26, '11 05:56:32PM

How do you activate this service in 10.5?



[ Reply to This | # ]
10.6: Create new AppleScript document Service
Authored by: diorite on Mar 24, '11 01:53:05PM

Nice tip. Too bad more examples on the web aren't URL encoded to make opening in Script Editor a one-click action.

Example:
applescript://com.apple.scripteditor?action=new&script=set%20whatItem%20to%20(do%20shell%20script%20%22diskutil%20list%20%7C%20grep%20'My%20HD%20Name'%22)%0Ddo%20shell%20script%20%22diskutil%20mount%20%22%20&%20(last%20word%20of%20whatItem)

There can be no spaces in the URL string.

As a clickable link, this opens Script Editor with a script called "Mount a connected firewire drive" in its window. Always nice when someone takes a moment for this extra touch.

D



[ Reply to This | # ]
applescript://com.apple.scripteditor?action=new&script=
Authored by: xhinter on Mar 26, '11 06:01:22PM
Well, this is actually the hint.
crarko, could you integrate it in the site?

[ Reply to This | # ]
10.6: Create new AppleScript document Service
Authored by: Maxikubik on Mar 30, '11 04:53:36AM
Cool, then we can simply boil the service AppleScript down to this:
on run {input, parameters}
	open location "applescript://com.apple.scripteditor?action=new&script=" & (input as string)
	return input
end run


[ Reply to This | # ]
10.6: Create new AppleScript document Service
Authored by: Maxikubik on Mar 25, '11 05:36:24AM

I did not know there was already a service for this, even though I looked for it (not closely enough apparently). I'm running 10.6 and I found it under the development services just like crarko adds above.

By the way, here is an update to my original hint above, preventing script failure when AppleScript Editor is running with no open documents:


on run {input, parameters}
	
	tell application "AppleScript Editor"
		activate
		if (count documents) > 0 and contents of document 1 is "" then
			set contents of document 1 to (input as string)
		else
			make new document with properties {contents:(input as string)}
		end if
	end tell
	
	return input
end run


[ Reply to This | # ]
10.6: Create new AppleScript document Service
Authored by: Lri on Mar 26, '11 03:53:44AM
ASE behaves strangely with make new document with properties {contents:(input as string)}.

For some reason it checks for errors. Sometimes the dialog opens behind the created document, sometimes it doesn't appear at all.

This seemed to fix it:
set doc to make new document
set text of doc to input


[ Reply to This | # ]
10.6: Create new AppleScript document Service
Authored by: Lri on Mar 26, '11 04:16:48AM
Another version:
-- 'Service receives selected: text' could still be more convenient
tell application "System Events"
	set old to the clipboard
	set proc to process 1 where frontmost is true
	try
		tell proc
			click menu bar 1's menu bar item "Edit"'s menu 1's menu item "Copy"
		end tell
	on error
		try
			delay 0.3 -- time to release modifier keys if triggered with a shortcut
			keystroke "c" using {command down}
		on error
			return
		end try
	end try
	delay 0.05
	set x to the clipboard as text
end tell

tell application "AppleScript Editor"
	-- for some reason, 'with properties {contents:x}' checks for errors
	-- sometimes the dialog appears behind the new window, sometimes nowhere
	set doc to make new document
	set text of doc to x
	set bounds of window 1 to {420, 22, 1500, 1100}
	activate
end tell

set the clipboard to old
The biggest difference is that it uses the oldskool way for getting the selection, so it doesn't have to be saved as an Automator service.

[ Reply to This | # ]