Script does not work in Automator

I have a well-functioning script that searches for a text selection in a previously opened Chrome tab whose URL starts with “https://www.google.com/search?q=…”) or in a new tab in case there was no tab whose URL starts with the indicated string (the purpose is not to have to open a new tab for each search performed in Chrome).

I have tried to use this same script as part of an Automator service (to associate it with the pressing of a key combination).

When running the service from the Automator window I get an error: “Some data cannot be converted into the expected type”.
However, if you run from Automator only the action corresponding to the script, no error occurs and it performs what is expected of it.

I do not understand the cause responsible for the error and would appreciate any help.


	tell application "System Events" to keystroke "c" using command down 
	delay 0.2
	
	set domain to "https://www.google.com/search?q="
	set _URL to domain & (the clipboard as text)
	
	tell application "Google Chrome"
		activate
		
		repeat with w in windows
			set i to 1
			
			repeat with t in tabs of w
				if URL of t starts with domain then
					set active tab index of w to i
					set index of w to 1
					tell its window 1
						activate
					end tell
					
					set URL of active tab of window 1 to _URL
					--delay 0.0
					
					return
				end if
				set i to i + 1 
			end repeat
						
		end repeat
		
		
		tell application "Google Chrome" to tell front window to make new tab at after (get active tab) with properties {URL:_URL} 	
		--	activate
		
	end tell


The reason for this different behaviour is quite understandable:

  1. you use GUI scripting to copy the selected text to the clipboard (although there is no need for this at all, because Automator services determine the selected text in a better way: The Action Receives: Text in: Google Chrome.app). The selected text very nice goes to input variable of action handler. No need keystrokes, System Events, clipboard and vitamins (activate commands or, set frontmost of process “Google Chrome” to true, and so on).

  2. Using this unneeded “bad GUI keystroke method”, then you don’t bring the Google Chrome.app window to the front (with activate command) before the GUI keystroke. When you perform an action in Automator, naturally the front application is it, not Google Chrome. So, your script keystrokes inside the Automator and you have what you have.

Hello, KniazidisR

Thank you very much for answering my question.
A few hours ago you sent me a reference to a post to better understand your answer.

Unfortunately now I am not able to find your message, so I cannot read the post you advise me.

Please resend it to me again.

Regards.

  1. Choose to Create new Quick Action (that is, the service).

  2. Adjust this settings: Workflow receives current: text in Any application.

  3. Add AppleScript:


on run {input, parameters}
	set domain to "https://www.google.com/search?q="
	set _URL to domain & input  -- THIS
	
	tell application "Google Chrome"
		activate
		-- YOUR REST CODE
	end tell
	return input
end run

I don’t know the application were you select the text. You can indicate the certain application instead of any application.

Hello, KniazidisR.

Your code works perfectly and I think I have understood the reasons why a code that works in a .scpt file, stops working when it is part of an Automator flow . Thank you very much. :smiley:

I have tried to generalize the structure of the script to be valid for other URLs that are frequently searched, in addition to the Google search engine or Wikipedia, and I think I have managed to solve the error that occurs when trying to adapt, in particular, to YouTube.

I think it is due to the differences that some URLs have in the URL string to perform the search (https://www.youtube.com/results?search_query=…) and the string that presents the URL once they have performed the search (https://www.youtube.com/watch?v=…).



on run {input, parameters}
	
	set domain to "www.youtube.com"  -- Modified
	set _URL to "http://www.youtube.com/results?search_query=" & input
	
	tell application "Google Chrome"
		activate
		
		repeat with w in windows
			set i to 1
			repeat with t in tabs of w
				if URL of t contains domain then  -- Modified
					set active tab index of w to i
					set index of w to 1
					tell its window 1
						activate
					end tell
					set URL of active tab of window 1 to _URL
					
					return
				end if
				set i to i + 1
			end repeat
		end repeat
			
		tell application "Google Chrome" to tell front window to make new tab at after (get active tab) with properties {URL:_URL}
		--    activate	
	end tell

end run

Thank you very much, again, for sharing your knowledge.