Jump to content

How to use subroutines/functions in Applescript workflows?


Recommended Posts

Hi,

 

Can I use subroutines in NSAppleScript Alfred workflows? I can't get this code to work:

on alfred_script(q)

tell application "Google Chrome"
	repeat with t in tabs of windows
		tell t
			if URL starts with "http://www.youtube.com/watch" or URL starts with "https://www.youtube.com/watch" then

				set new_URL to URL			
				set new_URL to replace_chars(URL, "youtube", "yourepeat")
				do shell script "/usr/bin/open -a '/Applications/Google Chrome.app' " & new_URL
			
			end if
		end tell
	end repeat
end tell

end alfred_script


to replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

Trying to debug the issue, I pasted the code in Automator (and made a few changes of course), and here's the syntax error I get:

Google Chrome got an error: item 1 of every tab of every window doesn’t understand the “replace_chars” message.

I tried putting the 'to replace_chars' subroutine in other places in the code, but it doesn't work.

 

Any help would be grealtly appreciated it.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...