The script checks whether an AppleScript applet is running. If not, it launches the applet and calls handler inside. If it is running, the applet is brought to the front and a text field value is set to the URL. So "pastes" is the wrong word - the Service uses GUI Scripting to set the value of a UI element of the target dialog or runs a handler in the target applet. Thus, it is the automator workflow that is doing to GUI scripting not the target applet.
But, workflows "as Services" cannot be granted assistive access so, the GUI scripting part fails.
Since my post, I have succeeded in registering an applet, containing the script, as a Service which is also granted assistive access. I manually edited the info.plist file to add NSService dictionary details so that the applet could be registered as a service. But, that applet has to be manually registered in the Keyboards pref pane before it appears in the Services menu - just putting a copy in the Services folder didn't work. Also, running it inside Safari takes over a minute before the target applet is launched or brought to the front. So, I'm not much advanced.
This is the script which is inside the Automator workflow (and the applet registered as a Service):
set app_name to short name of (info for (path to frontmost application))
if app_name is "Safari" then
using terms from application "Safari"
tell application app_name
set current_URL to URL of current tab of first window
end tell
end using terms from
else if app_name is "Firefox" then
tell application app_name to activate
tell application "System Events"
keystroke "l" using command down
keystroke "c" using command down
end tell
delay 0.5
set current_URL to the clipboard
else if app_name is "Opera" then
tell application app_name
set current_URL to URL of front document as string
end tell
end if
set MacYTDL_appName to "MacYTDL"
set startIt to false
-- Is MacYTDL running
tell application "System Events"
if not (exists process MacYTDL_appName) then
set startIt to true
end if
end tell
-- MacYTDL is not running - launch it and pass the URL to main dialog
if startIt then
tell application MacYTDL_appName
ignoring application responses
launch
called_by_service(current_URL)
end ignoring
end tell
end if
-- MacYTDL is running - bring it to the front...
if startIt is false then
tell application "System Events"
if not frontmost of process MacYTDL_appName then
set frontmost of process MacYTDL_appName to true
set value of text field 1 of window of process MacYTDL_appName to current_URL
end if
end tell
end if
Many thanks,
Garry