 |
 |
AppleScript and string conversion
|
 |
|
 |
|
Mac Enthusiast
Join Date: Apr 2000
Location: Belgium
Status:
Offline
|
|
I'm starting to really, really loathe AppleScript and its various annoying quirks. I'm trying to process my iTunes library though wbmp-count (a CLI utility that attempts to measure the BPM of music, I pipe mpg123's output through it in a shell script). This is my script:
Code:
tell application "iTunes"
copy the selection to thesel
repeat with x in thesel
copy location of x to theloc
copy (do shell script "/usr/local/bin/wbpm-countmp3.sh " & quoted form of POSIX path of theloc) to thebpm
copy thebpm as string to thebpm
return round of (thebpm)
end repeat
end tell
However, it keeps telling me that it can't convert thebpm ("138.992") to the expected type. What's up with that? The output of 'wbpm-countmp3.sh' is the bpm with a . for the decimal point, followed by a newline. Thanks for any tips you give.
|
|
PowerMac G4 400MHz/832MB/60GB
AlBook G4 15" 1.25GHz/1.5GB/60GB
Athlon 64 3500+/Asus A8N-SLI Premium/2GB RAM/990GB HD/GF7800GT 512
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Oct 2000
Location: Midwest
Status:
Offline
|
|
You have referred to some apps andprocesses that are unfamiliar, but I'd suggest changing the line:
copy thepm as string to thebpm
to:
set thebpm to (thebpm as text)
The differences between copy and set are technical and usually transparent. Set is the more often used for a variety of reasons. Text is a bit faster than string, string looks character by character at the object.
Craig
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Oct 2001
Location: London
Status:
Offline
|
|
I'm guessing the problem is the following lines
Code:
copy thebpm as string to thebpm
return round of (thebpm)
You are taking a string and casting it as a string (redundant) then trying to round a string.
(you want to cast it to a number instead)
I tried this in ScriptEditor and go 11 back -
Code:
set foo to "11.34"
set bar to foo as number
return round of (bar)
p.s. you might want to use "set" instead of "copy" (I think)
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Apr 2000
Location: Belgium
Status:
Offline
|
|
I tried both suggestions. The former results in the same error, whereas the latter ( set thebpm to (thebpm as number) ) says 'Can't make "132.003" into a number.'
The script looks like this now:
Code:
tell application "iTunes"
copy the selection to thesel
repeat with x in thesel
copy location of x to theloc
copy (do shell script "/usr/local/bin/wbpm-countmp3.sh " & quoted form of POSIX path of theloc) to thebpm
set thebpm to word 1 of thebpm
set thebpm to (thebpm as number)
return round of (thebpm)
end repeat
end tell
I added the 'set thebpm to word 1 of thebpm' to get rid of the newline in case that's causing this, but still no go.
|
|
PowerMac G4 400MHz/832MB/60GB
AlBook G4 15" 1.25GHz/1.5GB/60GB
Athlon 64 3500+/Asus A8N-SLI Premium/2GB RAM/990GB HD/GF7800GT 512
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Oct 2001
Location: London
Status:
Offline
|
|
What do you get in the Event Log if you add the following line below the Shell Script line?
log (class of thebpm)
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Apr 2000
Location: Belgium
Status:
Offline
|
|
I got it to work... sort of:
Code:
tell application "iTunes"
copy the selection to thesel
repeat with x in thesel
copy location of x to theloc
copy (do shell script "/usr/local/bin/wbpm-countmp3.sh " & quoted form of POSIX path of theloc) to thebpm
set thebpm to word 1 of thebpm
set AppleScript's text item delimiters to {"."}
set rbpm to ((text item 1 of thebpm)) as number
if ((text item 2 of thebpm) as number) > 499 then
add 1 to rbpm
end if
set the bpm of x to rbpm
end repeat
end tell
It's ugly, but that's the only thing that'll make it work.
|
|
PowerMac G4 400MHz/832MB/60GB
AlBook G4 15" 1.25GHz/1.5GB/60GB
Athlon 64 3500+/Asus A8N-SLI Premium/2GB RAM/990GB HD/GF7800GT 512
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Apr 2000
Location: Belgium
Status:
Offline
|
|
Originally posted by Diggory Laycock:
What do you get in the Event Log if you add the following line below the Shell Script line?
log (class of thebpm)
*Unicode text*
|
|
PowerMac G4 400MHz/832MB/60GB
AlBook G4 15" 1.25GHz/1.5GB/60GB
Athlon 64 3500+/Asus A8N-SLI Premium/2GB RAM/990GB HD/GF7800GT 512
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Apr 2000
Location: Belgium
Status:
Offline
|
|
Originally posted by Evinyatar:
It's ugly, but that's the only thing that'll make it work.
Okay, I take it back. Apparently the second text item of thebpm won't convert to a number.
|
|
PowerMac G4 400MHz/832MB/60GB
AlBook G4 15" 1.25GHz/1.5GB/60GB
Athlon 64 3500+/Asus A8N-SLI Premium/2GB RAM/990GB HD/GF7800GT 512
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Oct 2001
Location: London
Status:
Offline
|
|
Just another experiment here:
what happens if you run this script (replace ~/testscript.sh with your script (and a path to some mp3)
Code:
set theShellResult to ((do shell script "~/testscript.sh") as text)
return round (theShellResult as number)
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Apr 2000
Location: Belgium
Status:
Offline
|
|
Originally posted by Diggory Laycock:
Just another experiment here:
what happens if you run this script (replace ~/testscript.sh with your script (and a path to some mp3)
Code:
set theShellResult to ((do shell script "~/testscript.sh") as text)
return round (theShellResult as number)
Can't make "149.154" into a number.
|
|
PowerMac G4 400MHz/832MB/60GB
AlBook G4 15" 1.25GHz/1.5GB/60GB
Athlon 64 3500+/Asus A8N-SLI Premium/2GB RAM/990GB HD/GF7800GT 512
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Oct 2001
Location: London
Status:
Offline
|
|
Hmm - Sorry, I have no idea why it's doing that. I'm out of ideas... 
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Apr 2000
Location: Belgium
Status:
Offline
|
|
Well, I think I have an idea. AppleScript is horribly buggy with unicode text. Sometimes wbpm-count returns a whole number (Beautiful by Christina Aguilera returns 140), and in those cases the whole window server just crashes. Not good.
|
|
PowerMac G4 400MHz/832MB/60GB
AlBook G4 15" 1.25GHz/1.5GB/60GB
Athlon 64 3500+/Asus A8N-SLI Premium/2GB RAM/990GB HD/GF7800GT 512
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|