Trouble-Create a new Task in Things from "any" browser via AppleScript

I have the first script working but it is only for Chrome and Firefox. I would like it to work for Safari as well. Since I trigger this script via a hotkey set in Alfred, I would also like it to do nothing if I accidentally hit the hotkey. I am a total newbie to both applescript and programming in general so I assume I am breaking some basic rule but I have done everything via reverse engineering of other scripts written for other purposes.

Here is the working one…


(*Originally based on script by Ansel Halliburton (ansel@alum.berkeley.edu)

For reference, see:
http://culturedcode.com/things/wiki/index.php/AppleMailToThings
http://justinblanton.com/2010/09/blogging-textmate-chromium
http://chrome.blogspot.com/2010/10/bringing-another-chrome-release-to-you.html

*)

tell application "System Events"
	set FrontAppName to name of first process where frontmost is true
end tell

if FrontAppName is ("Google Chrome") then
	tell application "Google Chrome"
		set pageURI to (get URL of (active tab) of window 1)
		set pageTitle to (get title of (active tab) of window 1)
		set todo_notes to "[url= & pageURI & ]" & pageURI & "[/url]"
	end tell
	
else
	
	tell application "Firefox" to activate
	tell application "System Events"
		keystroke "l" using {command down}
		keystroke "c" using {command down}
		set pageURI to (the clipboard)
	end tell
	
	tell application "Firefox"
		set pageTitle to name of window 1
		set todo_notes to "[url= & pageURI & ]" & pageURI & "[/url]"
	end tell
end if

tell application "Things"
	set newToDo to make new to do ¬
		with properties {name:"Website: " & pageTitle}
	
	set tag names of newToDo to "Website,Read"
	set newToDo's notes to todo_notes
	
end tell

tell application "GrowlHelperApp"
	set the allNotificationsList to ¬
		{"Task Created"}
	set the enabledNotificationsList to ¬
		{"Task Created"}
	register as application ¬
		"Create Task from Multiple Mail (Growl Enabled)" all notifications allNotificationsList ¬
		default notifications enabledNotificationsList ¬
		icon of application "Things"
	notify with name ¬
		"Task Created" title ¬
		"Task created in Things" description ¬
		"Website: " & pageTitle application name "Create Task from Multiple Mail (Growl Enabled)"
	
end tell

This working script above,of course, undesirably activates Firefox if triggered in error.

The below is an attempt at getting it to work in all three primary browsers that I use.

Remember, I am a total beginner lacking foundation.


(*Script by Ansel Halliburton (ansel@alum.berkeley.edu)

For reference, see:
http://culturedcode.com/things/wiki/index.php/AppleMailToThings
http://justinblanton.com/2010/09/blogging-textmate-chromium
http://chrome.blogspot.com/2010/10/bringing-another-chrome-release-to-you.html

*)

tell application "System Events"
	set FrontAppName to name of first process where frontmost is true
end tell

if FrontAppName is ("Google Chrome") then
	tell application "Google Chrome"
		set pageURI to (get URL of (active tab) of window 1)
		set pageTitle to (get title of (active tab) of window 1)
		set todo_notes to "[url= & pageURI & ]" & pageURI & "[/url]"
	end tell
	
else
	
	if FrontAppName is ("Safari") then
		tell application "Safari"
			set pageURI to URL of current tab of window 1
			set pageTitle to name of window 1
			set todo_notes to "[url= & pageURI & ]" & pageURI & "[/url]"
		end tell
		
	else
		
		tell application "Firefox" to activate
		tell application "System Events"
			keystroke "l" using {command down}
			keystroke "c" using {command down}
			set pageURI to (the clipboard)
		end tell
		
		tell application "Firefox"
			set pageTitle to name of window 1
			set todo_notes to "[url= & pageURI & ]" & pageURI & "[/url]"
		end tell
	end if
end if

tell application "Things"
	set newToDo to make new to do ¬
		with properties {name:"Website: " & pageTitle}
	
	set tag names of newToDo to "Website,Read"
	set newToDo's notes to todo_notes
	
end tell

tell application "GrowlHelperApp"
	set the allNotificationsList to ¬
		{"Task Created"}
	set the enabledNotificationsList to ¬
		{"Task Created"}
	register as application ¬
		"Create Task from Multiple Mail (Growl Enabled)" all notifications allNotificationsList ¬
		default notifications enabledNotificationsList ¬
		icon of application "Things"
	notify with name ¬
		"Task Created" title ¬
		"Task created in Things" description ¬
		"Website: " & pageTitle application name "Create Task from Multiple Mail (Growl Enabled)"
	
end tell

Thank you for your help.

Curtis

Browser: Firefox 5.0.1
Operating System: Mac OS X (10.6)

I’ve edited your script to incorporate handlers. Here’s a tutorial on how they’re used: http://macscripter.net/viewtopic.php?id=24727

I’ve also replaced your if - else - else with the correct form which is if - else if - else if. Read up on if-statements here: http://macscripter.net/viewtopic.php?id=24757

I was actually puzzled why I couldn’t get process “Firefox” to do anything until I figured out that my Firefox process is called “firefox-bin” for some reason.

Morten


(*

Script by Ansel Halliburton (ansel@alum.berkeley.edu)

For reference, see:
http://culturedcode.com/things/wiki/index.php/AppleMailToThings
http://justinblanton.com/2010/09/blogging-textmate-chromium
http://chrome.blogspot.com/2010/10/bringing-another-chrome-release-to-you.html

*)

tell application "System Events"
	set FrontAppName to name of first process whose frontmost is true
end tell

if FrontAppName is ("Google Chrome") then
	tell application "Google Chrome"
		set pageURI to (get URL of (active tab) of window 1)
		set pageTitle to (get title of (active tab) of window 1)
		set todo_notes to "[url= & pageURI & ]" & pageURI & "[/url]"
	end tell
	addToThings(pageTitle, todo_notes)
	growlThis(pageTitle)
else if FrontAppName is ("Safari") then
	tell application "Safari"
		set pageURI to URL of current tab of window 1
		set pageTitle to name of window 1
		set todo_notes to "[url= & pageURI & ]" & pageURI & "[/url]"
	end tell
	addToThings(pageTitle, todo_notes)
	growlThis(pageTitle)
else if FrontAppName is ("firefox-bin") then
	tell application "Firefox" to activate
	tell application "System Events"
		keystroke "l" using {command down}
		keystroke "c" using {command down}
		set pageURI to (the clipboard)
	end tell
	tell application "Firefox"
		set pageTitle to name of window 1
		set todo_notes to "[url= & pageURI & ]" & pageURI & "[/url]"
	end tell
	addToThings(pageTitle, todo_notes)
	growlThis(pageTitle)
end if

on addToThings(pageTitle, todo_notes)
	tell application "Things"
		set newToDo to make new to do ¬
			with properties {name:"Website: " & pageTitle}
		
		set tag names of newToDo to "Website,Read"
		set newToDo's notes to todo_notes
	end tell
end addToThings

on growlThis(pageTitle)
	tell application "GrowlHelperApp"
		set the allNotificationsList to ¬
			{"Task Created"}
		set the enabledNotificationsList to ¬
			{"Task Created"}
		register as application ¬
			"Create Task from Multiple Mail (Growl Enabled)" all notifications allNotificationsList ¬
			default notifications enabledNotificationsList ¬
			icon of application "Things"
		notify with name ¬
			"Task Created" title ¬
			"Task created in Things" description ¬
			"Website: " & pageTitle application name "Create Task from Multiple Mail (Growl Enabled)"
	end tell
end growlThis

Wow! Works like a dream thanks. I will study up on your suggested resources.

Thanks again.

Curtis