John D
John D

Reputation: 159

How can I get a 'quoted form of' using a variable?

I have been working to create a script that iterates over files and is able to get the 'Keywords' metadata from the spotlight metadata. In getting help from others on stackoverflow, I have been able to get the metadata, but I cannot iterate over the files.

tell application "Finder"
set sourceFolder to folder POSIX file "/Users/jdavidson/Desktop/Upload/Birds and Butterflies/HighRes/"
set theFiles to files of sourceFolder
set inputPath to "/Users/jdavidson/Desktop/Upload/Birds and Butterflies/"

end tell

repeat with afile in theFiles
set filename to name of afile
set fname to text 1 thru ((offset of "." in filename) - 1) of filename
set pathVAR1 to "/Users/jdavidson/Desktop/Upload/Birds and Butterflies/HighRes/"
set pathVAR2 to pathVAR1 & filename
--set pathVAR3 to "\"" & pathVAR2 & "\""
set pathVAR3 to quoted form of pathVAR2

display dialog pathVAR3

set myvar to do shell script "mdls -name kMDItemKeywords " & pathVAR3

--Substring's the data to between "(" and ")"--
--set var1 to var1 + 1
set var2 to ((offset of ")" in myvar) - 1)
--set var2 to var2 - 1
set myKeywords to ((characters var1 thru var2 of myvar) as string)
display dialog myKeywords

end repeat

The code the does work and doesn't error out is as follows:

set myvar to do shell script "mdls -name kMDItemKeywords " & quoted form of "/Users/jdavidson/Desktop/Upload/Birds and Butterflies/To Process/Floral/RF78-3.tif"
set var1 to ((offset of "(" in myvar) + 1)
--set var1 to var1 + 1
set var2 to ((offset of ")" in myvar) - 1)
--set var2 to var2 - 1
set myKeywords to ((characters var1 thru var2 of myvar) as string)
display dialog myKeywords

The issue is being able to pass a variable to shell in 'quoted form of'

Upvotes: 2

Views: 372

Answers (2)

John D
John D

Reputation: 159

I rewrote everything and was able to get it work with the below code:

    set pathVAR1 to "/Users/johndavidson/Desktop/Upload/Temp/HighRes/"

    set pathVAR2 to pathVAR1 & filename

    set myvar to do shell script "mdls -name kMDItemKeywords " & quoted form of pathVAR2

    set var1 to ((offset of "(" in myvar) + 1)

    set var2 to ((length of myvar) - 1)

    set myKeywords to ((characters var1 thru var2 of myvar) as string)

Upvotes: 1

vadian
vadian

Reputation: 285082

It's easier to get the files in the folder as alias list and get the POSIX paths from the aliases.

path to desktop as text is the (HFS) path to the desktop of the current user

set sourceFolder to (path to desktop as text) & "Upload:Birds and Butterflies:HighRes:"
tell application "Finder"
    set theFiles to files of folder sourceFolder as alias list
end tell

repeat with aFile in theFiles
    set myvar to do shell script "mdls -name kMDItemKeywords " & quoted form of POSIX path of aFile
    --- ...
end repeat

Upvotes: 1

Related Questions