Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > AppleScript and string conversion

AppleScript and string conversion
Thread Tools
Mac Enthusiast
Join Date: Apr 2000
Location: Belgium
Status: Offline
Reply With Quote
Sep 10, 2003, 09:48 AM
 
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
Reply With Quote
Sep 10, 2003, 10:20 AM
 
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
Reply With Quote
Sep 10, 2003, 10:24 AM
 
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
Reply With Quote
Sep 10, 2003, 10:31 AM
 
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
Reply With Quote
Sep 10, 2003, 10:43 AM
 
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
Reply With Quote
Sep 10, 2003, 10:44 AM
 
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
Reply With Quote
Sep 10, 2003, 10:45 AM
 
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
Reply With Quote
Sep 10, 2003, 10:48 AM
 
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
Reply With Quote
Sep 10, 2003, 10:58 AM
 
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
Reply With Quote
Sep 10, 2003, 11:00 AM
 
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
Reply With Quote
Sep 10, 2003, 11:02 AM
 
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
Reply With Quote
Sep 10, 2003, 11:07 AM
 
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
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:08 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2