This is my first post, so please excuse poor formatting, etc.
I have been trying to write a script for iTunes that will remove the contents of field1 and append those contents to field2, where fields 1 and 2 are user selected from dialog boxes.
What I had (apparently foolishly) hoped to do was to write something like this pseudocode:
repeat with this_track in selection
set tmp to this_track's field1
set this_track's field1 to ""
set this_track's field2 to tmp
end repeat
I have searched everywhere I can find. It would appear that there is supposed to be some way to use “run script” to accomplish this seemingly trivial task, however, I cannot make any implementation or permutation of “run script” work at all.
Is there some way to accomplish this without resorting to endless blocked if-then statements? If it helps, I am an excellent Visual Basic and SAS programmer, I’m good with Javascript, and I am passable in PHP and Perl.
As an aside, it is hard to pine for a language as arcane as SAS (which is a statistical analysis package, btw), but it is sobering to realize that I could have written this code in SAS’s 30 year old mainframe-developed language in 60 seconds, vs. the rather embarrassing amount of time I have put in attempting to get this to work (partly because a number of solutions that remain on the web appear to apply to much older versions of the OS or Applescript).
Model: iMac
AppleScript: 2.2
Browser: Safari 535.1
Operating System: Mac OS X (10.7)
set field2 to "name"
set field1 to "track number"
do shell script "osascript -e 'tell app \"iTunes\"
repeat with t in (get selection)
tell t to set " & field2 & " to (" & field2 & " & " & field1 & ") as text
end
end'"
Using OSAScript is mainly used for backgrounding/forking, AppleScript from command line/shell script or when OS X isn’t available. Otherwise use run script.
run script "tell app \"iTunes\"
repeat with t in (get selection)
tell t to set " & field2 & " to (" & field2 & " & " & field1 & ") as text
end
end
On the other hand only eval when needed because you already know with PHP and JavaScript that evals have security leaks. Specially when you combine it with user variables, the same applies for AppleScript.
Just curious, in the previous code from DJ, I am assuming that everything you wrote needs to be wrapped in a handler, with various escaped quotes to complete the whole group as a single text item, right?
Yes that’s correct. Like in JavaScript and PHP if you want a double quote in a string you need to escape it. Also you need to escape the escape character of course. Depending on the script editor settings you can choose to escape tabs, line feeds and returns as well.
Run script doesn’t necessarily expects a string but when you want to eval you need to give a string as an argument (multiple arguments are possible).
Argh. Either I am totally daft, or something about Applescript just doesn’t jive with my brain. This code is working, albeit without the eval statement:
tell application "iTunes"
activate
set sel to selection
set these_tracks_ref to a reference to sel
set newDelimiter to " / "
set userDelimiter to " feat "
set field1 to "artist"
set field2 to "comments"
set fixed indexing to true
with timeout of 3600 seconds
repeat with i from 1 to count of sel
set this_track to item i of my these_tracks_ref
if userDelimiter is in this_track's artist then
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {userDelimiter}
set tagText to this_track's artist
set tagMovetext to (text items 2 through -1 of tagText) as text
set this_track's artist to (text item 1 of tagText) as text
set tmpComment to this_track's comment
set this_track's comment to tmpComment & my makenewtext(tmpComment, "Feat") & tagMovetext
set AppleScript's text item delimiters to oldDelims
end if
end repeat
end timeout
set fixed indexing to false
end tell
on makenewtext(someText, someTag)
if someText is "" then
set tmp to ""
else
set tmp to " / "
end if
return tmp & someTag & ": "
end makenewtext
Can anyone help me understand what would be necessary to take the meat of the program and parameterize it using “run script” so that when fields 1 and 2 are set by a user in a dialog box, the code executes as though all instances of “artist” were replaced with whatever was chosen as “field1” and all the instances of “comment” are replaced by whatever is chosen as field2?