如何使用变量获取“引用形式”?

时间:2019-04-10 18:29:25

标签: macos shell automation applescript

我一直在努力创建一个遍历文件的脚本,并且能够从Spotlight元数据中获取“关键字”元数据。在从其他人那里获得有关堆栈溢出的帮助时,我已经能够获取元数据,但是我无法遍历文件。

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

有效且不会出错的代码如下:

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

问题在于能够将变量以'的引号形式传递给shell

2 个答案:

答案 0 :(得分:1)

更容易以alias list的形式获取文件夹中的文件,并从别名中获取POSIX路径。

path to desktop as text是当前用户桌面的(HFS)路径

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

答案 1 :(得分:0)

我重写了所有内容,并能够通过以下代码使用它:

    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)