Trouble with paths...

I’m running into a problem, which should be simple enough to solve, but I’m having difficulty nonetheless.

Something simple, like so:

on open (list_of_aliases)
	repeat with i from 1 to number of items in list_of_aliases
		display dialog (POSIX path of item i of list_of_aliases)
	end repeat
end open

The problem I’m having is that if I have a file with a quote or space, such as “Foo’s File”, it’s not getting properly escaped. I end up with: /Users/foo/Foo’s File … instead of /Users/foo/Foo's\ File

If I use “quoted form of POSIX path …”, I end up with: ‘/Users/foo/Foo’"s File’ … and for whatever reason, it throws in an escaped double quote after the single quote instead of simply escaping the single quote.

Am I missing a step?


quoted form of is the right way, though it looks weird

Then I’m doing something wrong. Here, let me post the complete script (comments at bottom):

on open (list_of_aliases)
    set maxImageHeight to text returned of (display dialog "Max Height (in pixels):" default answer 500)
    set maxImageWidth to text returned of (display dialog "Max Width (in pixels):" default answer 500)
    repeat with i from 1 to number of items in list_of_aliases
        set theImage to item i of list_of_aliases
        set thePath to quoted form of POSIX path of theImage
        --set thePath to POSIX path of theImage
        set maxDpiHeight to 150
        set maxDpiWidth to 150
        set oldTids to text item delimiters
        set text item delimiters to {" "}
        -- Get the horizontal and vertial PPIs of the image.
        set dpiHeight to (last text item of (do shell script "sips -g dpiHeight " & thePath)) as integer
        set dpiWidth to (last text item of (do shell script "sips -g dpiWidth " & thePath)) as integer
        -- Get the height and width of the image
        set imageHeight to (last text item of (do shell script "sips -g pixelHeight " & thePath)) as integer
        set imageWidth to (last text item of (do shell script "sips -g pixelWidth " & thePath)) as integer
        set text item delimiters to oldTids
        if (dpiHeight > maxDpiHeight) or (dpiWidth > maxDpiWidth) or (imageHeight > maxImageHeight) or (imageWidth > maxImageWidth) then
            -- Set vertical PPI
            if dpiHeight > maxDpiHeight then
                set dpiHeight to maxDpiHeight
            end if
            -- Set horizontal PPI
            if dpiWidth > maxDpiWidth then
                set dpiWidth to maxDpiWidth
            end if
            -- Set image height
            if imageHeight > maxImageHeight then
                set imageFactor to (maxImageHeight / imageHeight)
                set imageHeight to maxImageHeight
                set imageWidth to imageWidth * imageFactor as integer
            end if
            -- Set image width
            if imageWidth > maxImageWidth then
                set imageFactor to (maxImageWidth / imageWidth)
                set imageWidth to maxImageWidth
                set imageHeight to imageHeight * imageFactor as integer
            end if
            -- DEBUG:: display dialog "New Dimensions: " & imageWidth & " x " & imageHeight
            -- Set up name of output file
            tell application "Finder"
                set fileName to the name of theImage
                set fileExtension to the name extension of theImage
                set fileName to text 1 thru -((length of fileExtension) + 2) of the fileName & "_new." & fileExtension
                set parentDir to text 1 thru -((length of fileName) - 2) of the thePath
                set newFilePath to parentDir & fileName & quote
            end tell
            set theCommand to "sips -s dpiHeight " & dpiHeight & " -s dpiWidth " & dpiWidth & " --resampleHeightWidth " & imageHeight & space & imageWidth & space & thePath & " --out " & newFilePath
            display dialog theCommand
        end if
    end repeat
end open

I’m using the path to the file as part of several “do shell script…” bits. And, as posted above, when it converts the file path, it’s either not properly escaping apostrophes and spaces, or in the quoted form, it’s leaving the apostrophe and adding an escaped double-quote for some reason. Like so:

'/Users/foo/Foo'\"s File'

actually the “escaped double quote” is an escaped single quote plus another single quote like

'/Users/foo/Foo' &  \' & 's File'

You should use the quoted form exclusively in shell scripts, not in expressions like

set parentDir to text 1 thru -((length of fileName) - 2) of the thePath

what exactly is going wrong in your script?

Aha. Thanks for brainstorming with me. It was indeed the…

set parentDir to text 1 thru -((length of fileName) - 2) of the thePath

…that was the culprit. I cleaned it up to:

set parentDir to (do shell script "dirname " & thePath)
set newFilePath to quoted form of (parentDir & "/" & fileName)

It is behaving as it should, now. That is, it shrinks and resamples an image to make it smaller. Made it for the kids in the journalism class since they were taking images straight from a camera (5-15 MB images, depending on the camera they were using) and putting them straight into the school newspaper (which is a PDF). This was making the school newspaper a 150-500 MB download. :smiley: The newspaper is now a much more manageable 10-15 MB.

As always, thanks again,