Help for an Xtra

An instant messenger which can connect to AIM, GTalk, Jabber, ICQ, and more.
Post Reply
Félix
Muffin
Posts: 39
Joined: Thu Apr 14, 2005 9:22 pm

Help for an Xtra

Post by Félix »

I made this and I'm sure I made an obvious error somewhere, but it doesn't work...

The goal of this Xtra is to send the current iTunes playing song (zipped to make it lighter).

Code: Select all

on substitute()
	try
		-- Is iTunes playing?
		tell application "System Events"
			set iTunes to ((application processes whose (name is equal to "iTunes")) count)
		end tell
		if iTunes is not greater than 0 then
			display dialog "iTunes' not playing!" buttons {"Ok"} default button 1
			return ""
		end if
		
		-- It IS playing, getting the track location (if any playing)
		tell application "iTunes"
			if the name of the current track is missing value then
				display dialog "No tune playing!" buttons {"Ok"} default button 1
				return ""
			end if
			set filepath to the location of the current track
			set trackname to the name of the current track
		end tell
		
		-- Zip It!
		do shell script "ditto -c -k " & filepath & " ~/Desktop" & trackname & ".zip"
		set tmppath to "~/Desktop" & trackname & ".zip"
		
		-- Ok, now we send it!
		tell application "Adium"
			tell active chat
				set buddy to ID
			end tell
			send (first chat whose ID is buddy) file tmppath
		end tell
	end try
end substitute
Warning: This message may contain traces of nuts.
User avatar
zaudragon
Growl Team
Posts: 1852
Joined: Sat Dec 04, 2004 5:05 am
Location: Kensington, CA, USA
Contact:

Re: Help for an Xtra

Post by zaudragon »

Code: Select all

on substitute()
	try
		-- Is iTunes playing?
		tell application "System Events"
			set iTunes to ((application processes whose (name is equal to "iTunes")) count)
		end tell
		if iTunes is not greater than 0 then
			display dialog "iTunes' not playing!" buttons {"Ok"} default button 1
			return ""
		end if
		
		-- It IS playing, getting the track location (if any playing)
		-- you need an else here, otherwise it'll open iTunes
		tell application "iTunes"
			if the name of the current track is missing value then
				display dialog "No tune playing!" buttons {"Ok"} default button 1
				return ""
			end if
			set filepath to the location of the current track
			set trackname to the name of the current track
		end tell
		
		-- Zip It!
		do shell script "ditto -c -k " & filepath & " ~/Desktop" & trackname & ".zip"
		set tmppath to "~/Desktop" & trackname & ".zip"
		
		-- Ok, now we send it!
		tell application "Adium"
			tell active chat
				set buddy to ID
			end tell
			send (first chat whose ID is buddy) file tmppath
		end tell
	end try
end substitute
I'm not sure how this WOULDN'T work. It looks fine to me…
Blog | X(tras)
Communists code without classes.
CASSIDY
Harmless
Posts: 15
Joined: Fri May 06, 2005 3:05 am

...

Post by CASSIDY »

Sory, stupid question but what is that for exactly?
I could sell RAID to a bug, Ima hustla I could sell salt to a slug cuzzzz
Reikon

Post by Reikon »

I'll look into it...
Reikon

Post by Reikon »

Ok...as for what was wrong with it:

1) The script ended before it began (when you have it return something, the script ends.)

2) you were trying to use Unix commands with ":" for a seperation instead of "/"

3) you weren't using "'" in case files names had more than one word. Example: ~/Desktop/song.zip works fine, however: ~/Desktop/song name.mp3 does not.

4) Tell active chat wasn't registering with Adium and returning an error, so I modified to ask from a list

Now, before you ask "WTF is all THAT!?"

Most of it is text manipulation.

Code: Select all

substitute()
on substitute()
	-- Is iTunes playing? 
	tell application "System Events"
		set iTunes to ((application processes whose (name is equal to "iTunes")) count)
		--If iTunes isn't running, let them know
		if iTunes is not greater than 0 then
			display dialog "iTunes' not playing!" buttons {"Ok"} default button 1
		else
			
			-- you need an else here, otherwise it'll open iTunes 
			tell application "iTunes"
				--If itunes is running but not playing, let them know
				if player state is stopped then
					display dialog "iTunes isn't playing!" buttons {"Ok"} default button 1
					--otherwise If itunes is playing, continue the script
				else if player state is not stopped then
					set filepath to the location of the current track
					set trackname to the name of the current track
					if trackname is equal to "missing value" then
						display dialog "No tune playing!" buttons {"Ok"} default button 1
					end if
				end if
			end tell
		end if
	end tell
	
	
	-- Zip It! 
	--set the contruction from applescript path ro unix path
	set filepath to quoted form of POSIX path of filepath
--add "'"'s so that it's an executable command
	set filepath to trim_line(filepath, "'", 0)
	set filepath to trim_line(filepath, "/Users/admin", 0)
	set j to replacesimple(filepath, "/", "'/'")
	set j to replacesimple(j, "'/'Music", "~/'Music")
	set filepath to j as string
	set x to "ditto -c -k -X -rsrc " & filepath & " ~/Desktop/'" & trackname & "'.zip" as string
	do shell script x
	--Adium uses Applescript and therefore :'s unlike Unix's /'s
	set deskpath to path to desktop as string
	set tmppath to deskpath & trackname & ".zip" as string
	
	-- Ok, now we send it! 
	tell application "Adium"
		tell every chat
			set mybuddies to ID
		end tell
		--There was an error with 'active chat' so I replaced it with choosing from list
		set chosenbuddy to (choose from list mybuddies with prompt "Which buddy do you want to send '" & trackname & "' to?" without multiple selections allowed) as text
		if chosenbuddy is not "false" then
			
			send (first chat whose ID is chosenbuddy) file tmppath
		end if
	end tell
	
	
end substitute
on trim_line(this_text, trim_chars, trim_indicator)
	-- 0 = beginning, 1 = end, 2 = both
	set x to the length of the trim_chars
	-- TRIM BEGINNING
	if the trim_indicator is in {0, 2} then
		repeat while this_text begins with the trim_chars
			try
				set this_text to characters (x + 1) thru -1 of this_text as string
			on error
				-- the text contains nothing but the trim characters
				return ""
			end try
		end repeat
	end if
	-- TRIM ENDING
	if the trim_indicator is in {1, 2} then
		repeat while this_text ends with the trim_chars
			try
				set this_text to characters 1 thru -(x + 1) of this_text as string
			on error
				-- the text contains nothing but the trim characters
				return ""
			end try
		end repeat
	end if
	return this_text
end trim_line
on replacesimple(thisText, oldChars, newChars)
	
	-- version 1.1, Daniel A. Shockley http://www.danshockley.com
	
	
	-- 1.1 coerces the newChars to a STRING, since other data types do not always coerce
	
	-- (example, replacing "nine" with 9 as number replaces with "")
	
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to the oldChars
	set the parsedList to every text item of thisText
	set AppleScript's text item delimiters to the {(newChars as string)}
	set the newText to the parsedList as string
	set AppleScript's text item delimiters to oldDelims
	return newText
	
	
end replacesimple
Not only does it compile, but it works. (i tested it)

As a side note, .zip compression really does nothing to .mp3s

the original file size is:

4741833 bytes or 5 MB

and the zipped file is:

4719440 bytes or 4.5 MB.

Enjoy!
Félix
Muffin
Posts: 39
Joined: Thu Apr 14, 2005 9:22 pm

Post by Félix »

Well actually it gives me a shell error, but I should be OK from this point :)

Thanks a lot :)

(I know ZIP compression does about nothing to MP3s, but AIF and MID files (less popular, true), can be compressed very well)

[EDIT] Well, I'm done now :) I use another way to change the string path, maybe less "beautiful", but still works very well.
It works, and I'd be happy to post it on Adium Xtras... But I can't now cuzz of the server changes. :(
Warning: This message may contain traces of nuts.
Reikon

Post by Reikon »

Félix wrote:Well actually it gives me a shell error, but I should be OK from this point :)

Thanks a lot :)

(I know ZIP compression does about nothing to MP3s, but AIF and MID files (less popular, true), can be compressed very well)

[EDIT] Well, I'm done now :) I use another way to change the string path, maybe less "beautiful", but still works very well.
It works, and I'd be happy to post it on Adium Xtras... But I can't now cuzz of the server changes. :(
Odd. Works perfect for me.
Post Reply