Reusing code

I have an applescript that uses the same piece of code in two places. This is obviously a bit of a pain having to make the changes in two places. How could I be able to reuse the code?

using terms from application "Quicksilver"
	on process text tweet
		my growlRegister()
		set charcount_limit to 140
		set charcount_tweet to (count characters of tweet)
		set over_limit to (charcount_tweet - charcount_limit)
		
		-- Check tweet length
		if charcount_tweet ≤ charcount_limit then
			--Display sending notification
			growlNotify("Sending Tweet", tweet)
			
			-- Log into twiter.com
			tell application "Keychain Scripting"
				set twitter_key to first Internet key of current keychain whose server is "twitter.com"
				set twitter_login to quoted form of (account of twitter_key & ":" & password of twitter_key)
			end tell
			
			set twitter_status to quoted form of ("status=" & tweet)
			try
				-- Send tweet
				do shell script "curl --user " & twitter_login & " --data-binary " & twitter_status & " [url=http://twitter.com/statuses/update.json]http://twitter.com/statuses/update.json"[/url]
				-- Display success message
				growlNotify("Tweet Sent", tweet)
			on error
				-- Display error message
				growlNotify("Error Sending Tweet", "There was an Error sending your Tweet. No, I don't know what went wrong either. Try again!")
			end try
		else
			
			set dialog_display to "Your tweet is longer than the 140 character limit by " & over_limit
			activate
			display dialog dialog_display & " characters. Your entire message can't be delivered by sms." buttons {"Edit tweet", "Post tweet anyway"} default button 1 with icon 2
			if the button returned of the result is "Post tweet anyway" then
				--Display sending notification
				growlNotify("Sending Tweet", tweet)
				
				-- Log into twiter.com
				tell application "Keychain Scripting"
					set twitter_key to first Internet key of current keychain whose server is "twitter.com"
					set twitter_login to quoted form of (account of twitter_key & ":" & password of twitter_key)
				end tell
				
				set twitter_status to quoted form of ("status=" & tweet)
				try
					-- Send tweet
					do shell script "curl --user " & twitter_login & " --data-binary " & twitter_status & " [url=http://twitter.com/statuses/update.json]http://twitter.com/statuses/update.json"[/url]
					-- Display success message
					growlNotify("Tweet Sent", tweet)
				on error
					-- Display error message
					growlNotify("Error Sending Tweet", "There was an Error sending your Tweet. No, I don't know what went wrong either. Try again!")
				end try
			else
				set selection of application "Quicksilver" to tweet
				
				
				
			end if
		end if
	end process text
end using terms from

As you can see, I’ll basically be able to cut my script’s size by half if I can reuse that big block of code. I assume that this will also make the script run faster too? This is only part of a bigger script than can run slowly on older machines.

Thanks in advance
Graham

Hi Graham,

just use a handler an call it twice.
I don’t know whether status= and tweet are global variables or properties,
If they are, no parameter passing is needed

on twitter_login(status=, tweet)
	--Display sending notification
	growlNotify("Sending Tweet", tweet)
				
	-- Log into twiter.com
	tell application "Keychain Scripting"
		set twitter_key to first Internet key of current keychain whose server is "twitter.com"
		set twitter_login to quoted form of (account of twitter_key & ":" & password of twitter_key)
	end tell
				
	set twitter_status to quoted form of ("status=" & tweet)
	try
		-- Send tweet
		do shell script "curl --user " & twitter_login & " --data-binary " & twitter_status & " [url=http://twitter.com/statuses/update.json]http://twitter.com/statuses/update.json"[/url]
		-- Display success message
		growlNotify("Tweet Sent", tweet)
	on error
		-- Display error message
		growlNotify("Error Sending Tweet", "There was an Error sending your Tweet. No, I don't know what went wrong either. Try again!")
	end try
end twitter_login

and the call is:

twitter_login(status=, tweet)

if a handler call is within a tell block, use my handler().
The parentheses are mandatory, even if no parameter is passed.
The names of the parameters in the handler could (and should) be different from those in the call,
within the handler they are only local variables.
The order of the parameters must be the same in this mode

What you do is make the main part of your program a subroutine. e.g. this could be a subroutine:


-- displays a dialog of text the_text
on DisplayDialog(the_text)
	display dialog the_text
	return
end DisplayDialog

You could load subroutines with the load script scripting addition.

set S to load script subroutine_reference
tell S
DisplayDialog(“hello”)
end tell

The hardest part beginning AppleScript people have is creating references.

Edited: btw, you need to think abstractly. Abstract thinking opens new avenues.

gl,

Thanks for the help, but I’m totally confused…

This is my complete script (what I started with)

using terms from application "Quicksilver"
	on process text tweet
		my growlRegister()
		set charcount_limit to 140
		set charcount_tweet to (count characters of tweet)
		set over_limit to (charcount_tweet - charcount_limit)
		
		-- Check tweet length
		if charcount_tweet ≤ charcount_limit then
			--Display sending notification
			growlNotify("Sending Tweet", tweet)
			
			-- Log into twiter.com
			tell application "Keychain Scripting"
				set twitter_key to first Internet key of current keychain whose server is "twitter.com"
				set twitter_login to quoted form of (account of twitter_key & ":" & password of twitter_key)
			end tell
			
			set twitter_status to quoted form of ("status=" & tweet)
			try
				-- Send tweet
				do shell script "curl --user " & twitter_login & " --data-binary " & twitter_status & " [url=http://twitter.com/statuses/update.json]http://twitter.com/statuses/update.json"[/url]
				-- Display success message
				growlNotify("Tweet Sent", tweet)
			on error
				-- Display error message
				growlNotify("Error Sending Tweet", "There was an Error sending your Tweet. No, I don't know what went wrong either. Try again!")
			end try
		else
			
			set dialog_display to "Your tweet is longer than the 140 character limit by " & over_limit
			activate
			display dialog dialog_display & " characters. Your entire message can't be delivered by sms." buttons {"Edit tweet", "Post tweet anyway"} default button 1 with icon 2
			if the button returned of the result is "Post tweet anyway" then
				--Display sending notification
				growlNotify("Sending Tweet", tweet)
				
				-- Log into twiter.com
				tell application "Keychain Scripting"
					set twitter_key to first Internet key of current keychain whose server is "twitter.com"
					set twitter_login to quoted form of (account of twitter_key & ":" & password of twitter_key)
				end tell
				
				set twitter_status to quoted form of ("status=" & tweet)
				try
					-- Send tweet
					do shell script "curl --user " & twitter_login & " --data-binary " & twitter_status & " [url=http://twitter.com/statuses/update.json]http://twitter.com/statuses/update.json"[/url]
					-- Display success message
					growlNotify("Tweet Sent", tweet)
				on error
					-- Display error message
					growlNotify("Error Sending Tweet", "There was an Error sending your Tweet. No, I don't know what went wrong either. Try again!")
				end try
			else
				set selection of application "Quicksilver" to tweet
				
				
				
			end if
		end if
		
		
		-- is iChat open?
		tell application "System Events"
			set iChatOn to count (every process whose displayed name is "iChat")
		end tell
		
		-- is Adium open?
		tell application "System Events"
			set AdiumOn to count (every process whose displayed name is "Adium")
		end tell
		
		-- is Proteus open?
		--tell application "System Events"
		--	set ProteusOn to count (every process whose displayed name is "Proteus")
		--end tell
		
		-- is Skype open?
		tell application "System Events"
			set SkypeOn to count (every process whose displayed name is "Skype")
		end tell
		
		-- if iChat is open, then tweet baby!
		if iChatOn is greater than 0 then
			try
				tell application "iChat"
					set the status message to tweet
				end tell
			on error
				growlNotify("iChat Failled", "Chainging iChat status failled.")
			end try
		end if
		
		
		-- Adium tweeting
		if AdiumOn is greater than 0 then
			try
				tell application "Adium"
					set my status message to tweet
				end tell
			on error
				growlNotify("Adium Failled", "Chainging Adium status failled.")
			end try
		end if
		
		
		-- Proteus tweeting
		--if ProteusOn is greater than 0 then
		--	try
		--		tell application "Proteus"
		--			set my current status to tweet
		--		end tell
		--	on error
		--		growlNotify("Proteus Failled", "Chainging Proteus status failled.")
		--	end try
		--end if
		
		-- Skype tweeting
		if SkypeOn is greater than 0 then
			try
				set SkypeText to "SET PROFILE MOOD_TEXT " & tweet
				tell application "Skype"
					send command SkypeText script name "SkypeTweet"
				end tell
				tell application "Quicksilver"
					set process text to tweet
				end tell
			on error
				grolwNotify("Skype Failled", "Changing Skype status failled.")
			end try
		end if
	end process text
end using terms from

using terms from application "GrowlHelperApp"
	-- Register Growl
	on growlRegister()
		tell application "GrowlHelperApp"
			register as application "Tweet" all notifications {"Alert"} default notifications {"Alert"} icon of application "Script Editor.app"
		end tell
	end growlRegister
	
	-- Notify using Growl
	-- Example: growlNotify("This is an Alert","This is a test of the Growl Alert System")
	on growlNotify(grrTitle, grrDescription)
		tell application "GrowlHelperApp"
			notify with name "Alert" title grrTitle description grrDescription application name "Tweet"
		end tell
	end growlNotify
end using terms from

And this is what I changed it to based on the above, which now doesn’t do a whole lot of anything :frowning:

on twitter_login()
	--Display sending notification
	growlNotify("Sending Tweet", tweet)
	
	-- Log into twiter.com
	tell application "Keychain Scripting"
		set twitter_key to first Internet key of current keychain whose server is "twitter.com"
		set twitter_login to quoted form of (account of twitter_key & ":" & password of twitter_key)
	end tell
	
	set twitter_status to quoted form of ("status=" & tweet)
	try
		-- Send tweet
		do shell script "curl --user " & twitter_login & " --data-binary " & twitter_status & " [url=http://twitter.com/statuses/update.json]http://twitter.com/statuses/update.json"[/url]
		-- Display success message
		growlNotify("Tweet Sent", tweet)
	on error
		-- Display error message
		growlNotify("Error Sending Tweet", "There was an Error sending your Tweet. No, I don't know what went wrong either. Try again!")
	end try
end twitter_login

using terms from application "Quicksilver"
	on process text tweet
		my growlRegister()
		set charcount_limit to 140
		set charcount_tweet to (count characters of tweet)
		set over_limit to (charcount_tweet - charcount_limit)
		
		
		
		-- Check tweet length
		if charcount_tweet ≤ charcount_limit then
			
			my twitter_login()
		else
			
			set dialog_display to "Your tweet is longer than the 140 character limit by " & over_limit
			activate
			display dialog dialog_display & " characters. Your entire message can't be delivered by sms." buttons {"Edit tweet", "Post tweet anyway"} default button 1 with icon 2
			if the button returned of the result is "Post tweet anyway" then
				twitter_login()
			else
				set selection of application "Quicksilver" to tweet
				
				
				
			end if
		end if
		
		
		-- is iChat open?
		tell application "System Events"
			set iChatOn to count (every process whose displayed name is "iChat")
		end tell
		
		-- is Adium open?
		tell application "System Events"
			set AdiumOn to count (every process whose displayed name is "Adium")
		end tell
		
		-- is Proteus open?
		--tell application "System Events"
		--	set ProteusOn to count (every process whose displayed name is "Proteus")
		--end tell
		
		-- is Skype open?
		tell application "System Events"
			set SkypeOn to count (every process whose displayed name is "Skype")
		end tell
		
		-- is aMSN open?
		tell application "System Events"
			set aMSNOn to count (every process whose displayed name is "aMSN")
		end tell
		
		-- if iChat is open, then tweet baby!
		if iChatOn is greater than 0 then
			try
				tell application "iChat"
					set the status message to tweet
				end tell
			on error
				growlNotify("iChat Failled", "Chainging iChat status failled.")
			end try
		end if
		
		
		-- Adium tweeting
		if AdiumOn is greater than 0 then
			try
				tell application "Adium"
					set my status message to tweet
				end tell
			on error
				growlNotify("Adium Failled", "Chainging Adium status failled.")
			end try
		end if
		
		
		-- Proteus tweeting
		--if ProteusOn is greater than 0 then
		--	try
		--		tell application "Proteus"
		--			set my current status to tweet
		--		end tell
		--	on error
		--		growlNotify("Proteus Failled", "Chainging Proteus status failled.")
		--	end try
		--end if
		
		-- Skype tweeting
		if SkypeOn is greater than 0 then
			try
				set SkypeText to "SET PROFILE MOOD_TEXT " & tweet
				tell application "Skype"
					send command SkypeText script name "SkypeTweet"
				end tell
				tell application "Quicksilver"
					set process text to tweet
				end tell
			on error
				grolwNotify("Skype Failled", "Changing Skype status failled.")
			end try
		end if
		
		-- aMSN tweeting
		if aMSNOn is greater than 0 then
			try
				
				tell application "aMSN"
					set aMSNText to "::MSN::changePSM " & tweet
					do script aMSNText
				end tell
			on error
				growlNotify("aMSN Failled", "Changing aMSN status failled.")
			end try
		end if
	end process text
end using terms from

using terms from application "GrowlHelperApp"
	-- Register Growl
	on growlRegister()
		tell application "GrowlHelperApp"
			register as application "Tweet" all notifications {"Alert"} default notifications {"Alert"} icon of application "Script Editor.app"
		end tell
	end growlRegister
	
	-- Notify using Growl
	-- Example: growlNotify("This is an Alert","This is a test of the Growl Alert System")
	on growlNotify(grrTitle, grrDescription)
		tell application "GrowlHelperApp"
			notify with name "Alert" title grrTitle description grrDescription application name "Tweet"
		end tell
	end growlNotify
end using terms from

I get the whole calling the name of the handler and stuff, it’s the variables and things that confused me. I don’t know what things are called, this is my first applescript project and it’s been totally trial and error so far…

Hi Graham,

I’m sorry, I don’t use neither Adium nor QuickSilver
so I can’t test the script (it doesn’t compile)

No matter, you helped loads anyway. I got it going on my own. I missed out the tweet part from twitter_login(tweet).

Thanks a lot!