Networking | Hardware | Software | Multimedia | System | Unix&Linux | MBA

Home>>Unix&Linux>>Escaping quotes in Tiger

Escaping quotes in Tiger

DougAdams
05-05-2005, 08:09 AM
I'll admit I am not the most adept quote-escaper when it comes to UNIX, but this has me baffled.

Since upgrading to Tiger, my AppleScript do shell scripts don't seem to escape quotes correctly.

I'm doing some stuff with mdfind (for Spotlight stuff). I'm grabbing song name, artist and album (nom, art, and alb, below) from an iTunes track and doing a search, like so:set the_command to ("mdfind " & "\"kMDItemTitle = " & quoted form of nom & " kMDItemAlbum = " & quoted form of alb & " kMDItemAuthors = " & quoted form of art & "\" ")

set returned_list to (do shell script the_command)


Whenever I encounter a string with an apostrophe it doesn't seem to escape properly. Or at the very least I get no result. Now, I'd swear this worked correctly in Panther--of course, there was no Spotlight, but other scripts using the same technique never balked.

So, to paraphrase Robert Oppenheimer, what am I doing wrong?

mark hunte
05-05-2005, 09:16 AM
In 10.3
I had a similar problem with : or ' in the song or album names

It was quicker taking them out than adjusting the script.

What is happening is the quotes you are using are not seeing the : ' as literal so it is trying to use it as part of the command.

One way to see this is put a backtick in a song name and run the script.

You will then get a clear message showing you whats going on.

If any one else wants to help in this, here is a addition to the above script for testing.

Select a song in iTunes and run the script.



tell application "iTunes"
set album_name to album of item 1 of selection


set Artist_name to artist of item 1 of selection



set Song_name to name of item 1 of selection


end tell



set the_command to ("mdfind " & "\"kMDItemTitle = " & quoted form of Song_name & " kMDItemAlbum = " & quoted form of album_name & " kMDItemAuthors = " & quoted form of Artist_name & "\" ")

set returned_list to (do shell script the_command)

DougAdams
05-05-2005, 10:06 AM
Thanks Mark

Here are the results with a song name that contains an apostrphe:

do shell script "mdfind \"kMDItemTitle = '(I Know) I'\\''m Losing You' kMDItemAlbum = 'Ecology' kMDItemAuthors = 'Rare Earth'\" "
""


Note the apostrophe in the song title. Now, the file does exist, but the command doesn't find it. Works with tags that don't have the "'", like so:

do shell script "mdfind \"kMDItemTitle = 'My Wave' kMDItemAlbum = 'Superunknown' kMDItemAuthors = 'Soundgarden'\" "
"/Volumes/Doug Adams’s iPod/iPod_Control/Music/F29/My Wave.mp3
/Volumes/G5 Main/Music/Soundgarden/Superunknown/My Wave.mp3"

Ooo, look. It found one on my iPod. (Thanks to Spodlight).

Anyway, when I type it in literally (I have manually escaped the apostrophe):

do shell script "mdfind \"kMDItemTitle = '(I Know) I\\'m Losing You' \" "

It works fine. So there seems to be a problem with the way escapes are being handled by "quoted form of" between AppleScript and the do shell script. Or am I off on this?

mark hunte
05-05-2005, 11:30 AM
Yes.. thats right, I am sure I have seen an answer some where in using a different method of quoting from AS,
but do you think I can find it..

DougAdams
05-06-2005, 06:01 AM
Here's a workaround, which I do not care for:

tell application "iTunes"
set album_name to album of item 1 of selection
set Artist_name to artist of item 1 of selection
set Song_name to name of item 1 of selection

set album_name to replace_chars(album_name, "'", "\\'")
set Artist_name to replace_chars(Artist_name, "'", "\\'")
set Song_name to replace_chars(Song_name, "'", "\\'")
end tell

set the_command to ("mdfind " & "\"kMDItemTitle == '" & Song_name & ?br /> "' kMDItemAlbum = '" & album_name & ?br /> "' kMDItemAuthors = '" & Artist_name & "'\" ")

set returned_list to (do shell script the_command)

on replace_chars(txt, srch, repl)
set AppleScript's text item delimiters to the srch
set the item_list to every text item of txt
set AppleScript's text item delimiters to the repl
set txt to the item_list as string
set AppleScript's text item delimiters to ""
return txt
end replace_chars


Oh well. Better than nothing, I guess.

mark hunte
05-06-2005, 07:12 AM
Does that work for you , does not work for me... comes up with an error

DougAdams
05-06-2005, 07:25 AM
Ooops, sorry. The calls to the "replace_chars" handler should be placed outside of the iTunes tell block. (Or, you could add "my " to the front of each handler name.)

mark hunte
05-06-2005, 12:38 PM
Ok That works.

Hey have you tried to actually lose a iTune file in Tiger.
I can NOT do it, no matter where I move it to, with iTunes running or not.
Even when the track is playing :)

AFAIK you could do this in Panther?? ( not that you wanted to)


anyone??

DougAdams
05-06-2005, 01:02 PM
What I have done to experiment with this snippet is to copy the file to the desktop (as a backup), then use Command-R on the track to show its file where iTunes expects it, then QUIT iTunes, delete the "revealed" file, and Empty the Trash. Then restart iTunes. iTunes can't find the file because it's not where it expects it! Run the script and it will point to the one on the desktop.

iTunes uses "alias technology" to follow the file if you move it to the Trash. But you can trick it by shutting it down before deleting the file.

This trick has always worked.

mark hunte
05-06-2005, 01:44 PM
By the way I had a look on the site a downloaded the "bring out yer dead"
(sure the should be "bring out yee dead")
and could not open the .sit file

Had Stuffit v901

Had to install v9.0.2 (fo 10.4 upwards)

which worked

DougAdams
05-08-2005, 11:17 AM
Yeah. StuffIt has to be re-installed in Tiger. Oh bother! No troubles afterwards, tho, as you you also report.

One of these days I'm gonna swap everything over to zip files. But with 300+ archives...well, you can imagine. Guess I should write an AppleScript ;)


 

TOP

Windows Server Outsell
Unix Signals And C++ E

For more info

Unix Signals And C++ E
Windows Server Outsell
ssh setup for password
Bash script does not w
esc code 
ARD send unix command 
question about binarie
Scanning mail 
Issuing multiple comma
How do I install Linux

News Archive

/etc/hosts? 
Manually Start a Start
mounting missed hard d
Using Netinfo in Singl
darwin/bsd login probl
system_profiler and fi
Send mail from script 
mounting a drive 
OS X disk first aid ha
system.log shows steal

Related stories:

How to set environment variables and path in 10.4?
problem installing Fink
pkg-config & PKG_CONFIG_PATH
Applescript: Hide Process
help with installing py-gnome (darwinports)
Mozilla install from dports fails
ssh - don't want to use password

Copyright@2004-2005 www.zzcoke.com All Right Reserved

advanced web statistics