Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 12, 2009, 04:24 PM   #1
odoyle81
macrumors newbie
 
Join Date: Mar 2008
applescript compare track names- if statement

I'm working on an itunes applescript to delete all podcasts on my ipod shuffle that have been listened to, and then add new ones from a smart playlist. The one part of the script that I need help with is some type of if statement to avoid copying a file from the smart playlist if it is already exists on the shuffle. I'm trying to do this by comparing the names of the tracks, but it doesn't seem to work. I'm new to applescript and I have a feeling I'm not using the right type of variables or I'm not building the statement correctly. Can anyone help me out with this? Thanks!!

The statement I'm trying to use is:
if (name of atrack is equal to value of anitem) then

Here is the code:
tell application "iTunes"
repeat with thisSource in sources
if the name of thisSource = "Paul" then set myIpod to thisSource
end repeat

set destinationPlaylist to the first playlist in myIpod
set sel to every track in destinationPlaylist whose podcast is true and played count is greater than 0
repeat with atrack in sel
delete atrack
end repeat

set sourcePlaylist to playlist "newpodcasts"
if the number of tracks in sourcePlaylist is greater than 0 then
set shufflable of (every track of sourcePlaylist) to true
if the number of tracks in destinationPlaylist is greater than 0 then
set shufflefilenames to (name of file tracks in destinationPlaylist whose podcast is true)
set newsel to (every track in sourcePlaylist)
repeat with atrack in newsel
repeat with anitem in shufflefilenames
if (name of atrack is equal to value of anitem) then
set donotdup to true
exit repeat
else
set donotdup to false
end if
end repeat
if donotdup is false then
duplicate atrack to destinationPlaylist
end if
end repeat
else
duplicate every track in sourcePlaylist to destinationPlaylist
end if

end if

display dialog "all podcasts have been updated and old ones have been deleted"
end tell
odoyle81 is offline   0 Reply With Quote
Old Apr 13, 2009, 12:42 PM   #2
LtRammstein
macrumors 6502a
 
Join Date: Jun 2006
Location: Denver, CO
Send a message via AIM to LtRammstein Send a message via MSN to LtRammstein
Let me clean it up so people can read it.

Code:
tell application "iTunes"
	repeat with thisSource in sources
		if the name of thisSource = "Paul" then set myIpod to thisSource
	end repeat

	set destinationPlaylist to the first playlist in myIpod
	set sel to every track in destinationPlaylist whose podcast is true and played count is greater than 0
	repeat with atrack in sel
		delete atrack
	end repeat

	set sourcePlaylist to playlist "newpodcasts"
	if the number of tracks in sourcePlaylist is greater than 0 then
		set shufflable of (every track of sourcePlaylist) to true
		if the number of tracks in destinationPlaylist is greater than 0 then
			set shufflefilenames to (name of file tracks in destinationPlaylist whose podcast is true)
			set newsel to (every track in sourcePlaylist)
			repeat with atrack in newsel
				repeat with anitem in shufflefilenames
					if (name of atrack is equal to value of anitem) then
						set donotdup to true
						exit repeat
					else
						set donotdup to false
					end if
				end repeat
				if donotdup is false then
					duplicate atrack to destinationPlaylist
				end if
			end repeat
		else
		duplicate every track in sourcePlaylist to destinationPlaylist
		end if

	end if

	display dialog "all podcasts have been updated and old ones have been deleted"
end tell
My suggestion:

After reading your code, I think you might have an issue at that if statement. What is "atrack" equal to and why do you delete it earlier in your code then try to read it later? Do you set this in a precompiler or what?

More clarification on your code is needed, mainly comments.
__________________
17" White Intel iMac 1.83GHz, 250 GB HD, 2 GB RAM || iPod Touch 16GB w/ 3.0 || 15" Intel MBP 2.4GHz, 250 GB HD, 4 GB RAM
LtRammstein is offline   0 Reply With Quote
Old Apr 13, 2009, 02:47 PM   #3
odoyle81
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
Thanks for the suggestions - I'm new to coding and I also couldn't figure out how to paste the code with the proper indentations. I worked on this for several more hours yesterday, and I got it working and just wanted to share the working code here.

Code:
tell application "iTunes"
	repeat with thisSource in sources
		if the name of thisSource = "Paul" then set myIpod to thisSource
	end repeat
	
-- this part deletes all the podcasts on the ipod shuffle which have been listened to
	set destinationPlaylist to the first playlist in myIpod
	set sel to every track in destinationPlaylist whose podcast is true and played count is greater than 0
	repeat with atrack in sel
		delete atrack
	end repeat
	
	-- "newpodcasts" is a smart playlist that only contains podcasts to be added
	set sourcePlaylist to playlist "newpodcasts"
-- if statement to only do this if there are new podcasts to add
	if the number of tracks in sourcePlaylist is greater than 0 then
		set shufflable of (every track of sourcePlaylist) to true
-- if statement to only do this if there are already tracks on the ipod shuffle
		if the number of tracks in destinationPlaylist is greater than 0 then
			set shufflefilenames to (name of file tracks in destinationPlaylist whose podcast is true)
			set newsel to (every track in sourcePlaylist)
-- check each track on the shuffle and see if it has the same name as a podcast about to be uploaded.  If it does, change a boolean variable called "donotdup" to true so this track WON'T be deleted
			repeat with atrack in newsel
				if name of atrack is in shufflefilenames then
					set donotdup to true
				else
					set donotdup to false
				end if
				if donotdup is false then
					duplicate atrack to destinationPlaylist
				end if
			end repeat
		else
			duplicate every track in sourcePlaylist to destinationPlaylist
		end if
		
	end if
	
	display dialog "all podcasts have been updated and old ones have been deleted"
end tell
odoyle81 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Applescript. If..then statement not working. urello Mac Programming 4 Mar 5, 2014 08:35 AM
Total Newb Here! - Help with Applescript & file names vapegirl18 Mac Programming 2 Feb 9, 2014 06:11 PM
AppleScript Previous Track wrignj08 Mac Programming 5 Aug 16, 2013 04:46 AM
applescript if statement wild4life2013 Mac Programming 2 Aug 6, 2013 12:16 AM
Get current track from Spotify in Messages status(AppleScript) pinyourwings22 Mac Programming 6 Mar 26, 2013 08:13 AM

Forum Jump

All times are GMT -5. The time now is 11:00 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC