"Run Applescript" in Automator

Is it possible to use SD to debug “Run Applescript” Automator actions?

Unfortunately, no. Automator’s Run AppleScript action does not allow you to select an OSA component (scripting system). For Script Debugger to get its hand on your code, you need to compile it using the AppleScript Debugging OSA component (scripting system).

Ok, I’m mostly confused by the classes of input I get, but I guess that’s a “read the documentation” issue… Btw, is there such documentation for Automator?

Jean Christophe,

Why not just post your automator question. I could probable answer your question from seeing the AppleScript put into the automator action.

Back when I first figured out how to use automator with applescripts I used “class of” a lot to figure what classes were actually being used in the script embedded in automator. Then I just used display dialog to display the classes or other useful things to help me figure out what was going on.

I write a lot of “text services” which involves AppleScripts embedded into an automator actions. A “text service” is something that is run on the frontmost window of an application when the window supports text services. The scripts show up in the application’s “Services” menu and are run when the automator action is selected from the “Services” menu. I included a full example of one of these text services. I think they would pretty much the same input as what you are working with.

If you want to try this sample out put it into the ~:Library:Services folder. This is the library folder in your user folder. If the services folder doesn’t exist then then you can create it in finder. If you have something that works it easier to figure things out. Just paste the applescript in the automator step.

There aren’t any recent automator books.

Here some older books still for sale:
Automator for Mac OS X 10.6 Snow Leopard: Visual QuickStart GuideNov 20, 2009 v

Apple Automator with AppleScript Bible Pap/Psc Edition by Myer, Thomas published by John Wiley & Sons (2009) paperback

Apple Automator with AppleScript Bible (2009)

Automator for Mac OS X 10.6 Snow Leopard: Visual QuickStart Guide (2009)
http://www.peachpit.com/store/automator-for-mac-os-x-10.6-snow-leopard-visual-quickstart-9780321685834

Easy Automator Cookbook Dec 12, 2013

Hopefully something in here will help.

use scripting additions
use framework "Foundation"

on changeCaseOfText(SourceText, CaseIndicator)
	-- CaseIndicator = 0 all uppercase, CaseIndicator = 1 all lowercase, CaseIndicator = 2 titlecase, 
	-- create a Cocoa string from the passed text, by calling the NSString class method stringWithString:
	
	script ReturnObj
		property Successful : false
		property FormattedStr : ""
	end script
	try
		set the sourceString to current application's NSString's stringWithString:SourceText
		-- apply the indicated transformation to the Cocoa string
		if the CaseIndicator is 0 then
			set the adjustedString to sourceString's uppercaseString()
		else if the CaseIndicator is 1 then
			set the adjustedString to sourceString's lowercaseString()
		else
			set the adjustedString to sourceString's capitalizedString()
		end if
		
		-- convert from Cocoa string to AppleScript string
		set (FormattedStr of ReturnObj) to (adjustedString as text)
		set (Successful of ReturnObj) to true
		return ReturnObj
	on error errMsg number errNum
		display dialog "Error " & (errNum as text) & " occurred while changing the case of the selected text." & return & return & errMsg buttons {"OK"} default button "OK" with title "Error"
		set (Successful of ReturnObj) to false
		return ReturnObj
	end try
end changeCaseOfText

on run {input, parameters}
	try
		set TheInput to input as text
		
		set TheResult to changeCaseOfText(TheInput, 1)
		if (Successful of TheResult) then
			set TextToDisplay to FormattedStr of TheResult
		else
			set TextToDisplay to TheInput -- Return old text
		end if
		
		return TextToDisplay
	on error errMsg number errNum
		display dialog "Error " & (errNum as text) & " occurred while preparing the selected text." & return & return & errMsg buttons {"OK"} default button "OK" with title "Error"
	end try
end run

Bill

1 Like

Thank you Bill. I’ve written a number of “run something script” services in the past, but just as everything, if you don’t practice a skill, you forget it.

I’m actually thinking a “Run Applescript” simulator would actually be a great feature to have in SD :slight_smile:

I was trying Automator because I could not figure out how to get the selected text of applications that dont let Applescript get the selection (or maybe I’m missing something trivial and any scriptable application can send the text selection to AS).

I discovered after some trials that the text input from “Any application” is a list and then proceeded from there. But it would greatly help to have a formal reference of what Automator sends to the action as input.

Jean Christophe,

Then what I sent you is perfect. It gets the selected text from an Application, BBEdit, Script Debugger, Numbers, Pages, FileMaker Pro ,… I will change the case on any of those apps and more.

I went ahead and enclosed 5 automator actions for text services in this post. 3 change case, 1 combines lines that end in returns in to a single paragraph. The last one will try to evaluate the selected text and if it’s a valid formula it will add an equal sign and the resulting value to the right of the formula. That should be enough to get you going again.

Bill

Text actions.zip (413.8 KB)

1 Like

Since the post must be 20 characters or more to be accepted, I need to add that prelude to say Thank you Bill ! :slight_smile:

Jean Christophe,

The nice thing about doing things this way is you don’y need AppleScript to get the current text selection in the application. Apples’s text services just gives you the selected text.

Bill

1 Like

So you confirm that there is a limitation in AS regarding the ability to get the selection in any application that supports text services, right ? It’s like they’re not compatible things ? (Sorry if my wording is not clear). Would something in ASObj-C be able to cover that “text services” thing ?

There are some cases where AppleScript can’t get the text selection but text services can get the selection. The 2 ways have different ways to get the selection. Text services is more reliable than AppleScript when it comes to getting the selected text.

Also you can set which application this will work on in Automator. You can also set it to run on all applications.

If you want the AppleScript inside the automator action to run differently depending on which application is front most just use

tell application "Finder" to set TheName to name of (path to frontmost application)

and TheName will be set to the name of the frontmost application. The font most application is alway the one the text services will run on.

Does this answer your question?

Bill

Partly, since there are applications that are supported by text services but not by AS, for ex, applications that support only the Text Suite but have nothing dedicated to serving the selection to AS. Is my understanding correct?

Some application don’t even try to implement getting the selection in AppleScript. These are usually very cheap applications or free applications.

You can go to the script debugger dictionary, open the dictionary for the app you’re trying to get the selection for, go to the top right of dictionary window where the search field is and type in selection then hit the return. I included an image of what this should look like. If something comes up for selection then command does exist. If nothing comes up it’s not implemented. If the selection command is implemented but you can’t get the selection even when something is selected the command is most likely broke. The company may fix such bugs if reported but some companies don’t worry too much about AppleScript errors.

Text services is used a lot by developers and scripters and it is something Apple will fix if it fails. So it is pretty reliable.

Bill

Thank you for the detailed explanations. I figured that if selection was not in the dictionary it meant AS had no access to it, but still the “selection” exists and is accessed by Text Services, does that mean that some ASObj-C wizardry can access it, or is it totally off limit to scripting and we’re stuck with Automator?

Wow !

https://developer.apple.com/library/content/documentation/AppleApplications/Conceptual/AutomatorConcepts/Articles/ImplementScriptAction.html#//apple_ref/doc/uid/TP40001512-CJBJEJBE

“The input parameter contains (in most instances) the output of the previous action in the workflow; it is almost always in the form of a list.”

almost always”

Text services has nothing to do with AppleScript, and Text services uses a completely different way to find the selected text that has nothing to do with AppleScript. So one can fail while the other works.

What all this does indicate is the sad condition some scriptable applications are in. Some developers think AppleScript is not something that effects the value of their software while other features in the application not working would reduce the value of the application, and therefore make it hard to sell their app for the normal retail price. So their development efforts go into what they think is important and does not go into things not considered important. I’ve worked with with Applescript for a few decades and I have seen this kind of thing a lot. But most of the really good apps do have decent implementations of AppleScript. It the cheaper apps that have messed up dictionaries.

Bill

1 Like

Jean Christophe,

I was thinking about the text services I was talking about with you and thought you might be interested in reading more. I called it text services but that is really 2 things. When talking about services it’s talking about things done in the “Services” menu which is in the application menu. Services can work with different things but when working with text in a document you want “text” services. That is what is meant by “text services.” It’s “services” that deal with “text.”

When the apple page talks about pasetboards for services they are talking about the kinds of things used for regular copy and paste stuff but in the case of a service data is transferred using a special pasetboard that has nothing to do with the regular copy and paste stuff users do from the Edit menu. But a special pasteboard is used to get text from the window, and to send back text to the window. I think you can follow the stuff on that page and it has a nice picture showing what is going on. But if the page doesn’t make sense let me know & I can explain further.

After reading that it should be pretty clear what the difference is between a text service and AppleScript. Don’t worry about the stuff that talks about a sandbox. It isn’t important to this discussion.

Here is Apple page that talks about it.
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/SysServices/Articles/overview.html#//apple_ref/doc/uid/20000850-97688

Bill

1 Like