在 applescript 中嵌套重复时的多个 if 条件
Multiple if conditions within nested repeat while in applescript
我以前从未使用过 AppleScript,所以我对这门语言很陌生,但我正在尽力而为。
这是我想要完成的:
运行 选择一个包含 .ARW 和 .JPG 文件的文件夹时的脚本。遍历文件夹中的项目。如果当前项目是 .ARW,则再次从头开始遍历文件夹。如果此嵌套迭代落在具有相同文件名和 JPG 扩展名的文件上,则将原始 ARW 文件标记为红色。
TLDR:如果文件夹中的 ARW 文件与文件夹中的 JPG 文件共享相同的文件名,则以红色突出显示 ARW 文件,否则什么都不做。
这是我到目前为止编写的代码:
tell application "Finder"
set totalAlias to the entire contents of (selection as alias)
set totalCount to the count of items of totalAlias
set firstName to name of item 1 of totalAlias
set firstExtension to name extension of item 1 of totalAlias
set c to 1
repeat while c ≤ totalCount
set currentAlias to item c of totalAlias
set currentName to name of currentAlias
set currentExtension to name extension of currentAlias
if currentExtension is "ARW" then
set d to 1
set compareFile to currentAlias
set findName to currentName
set findExtension to currentExtension
repeat while d ≤ totalCount
if (name of item d of totalAlias = findName) and (name extension of item d of totalAlias is "JPG") then
tell application "Finder" to set label index of compareFile to 2
end if
set d to (d + 1)
end repeat
end if
set c to (c + 1)
end repeat
end tell
有什么问题吗?我相信这与我的 IF AND 条件有关。
试试这个脚本:
tell application "Finder"
--Just get all the filenames of the target types:
set allJPG to the name of every file of (entire contents of (selection as alias)) whose name extension = "JPG"
set allARW to the name of every file of (entire contents of (selection as alias)) whose name extension = "ARW"
--Send the two lists to a handler to find all the common names
set targetJPGFiles to my CompareNames(allJPG, allARW)
--Loop through the common names, find the files, set the tags
repeat with eachTarget in targetJPGFiles
set fileToTag to (item 1 of (get every file of (entire contents of (selection as alias)) whose name is (eachTarget as text)))
set label index of fileToTag to 2
end repeat
end tell
targetJPGFiles -- This allows you to see the filenames that SHOULD have been tagged
to CompareNames(jp, pn)
--First, get rid of all the extensions in the ARW files
set cleanARWNames to {}
set neededJPGNames to {}
repeat with eachARWName in pn
set end of cleanARWNames to characters 1 thru -5 of (eachARWName as text) as text
end repeat
--Now, loop through JPG names to find a match
repeat with eachjpgName in jp
set searchName to characters 1 thru -5 of (eachjpgName as text) as text
if cleanARWNames contains searchName then
set end of neededJPGNames to (eachjpgName as text)
end if
end repeat
return neededJPGNames
end CompareNames
它采用了一种略有不同的方法,因为它只比较两个文件名列表,然后返回,找到具有所需名称的文件,并进行标记。
它基于我为另一个项目编写的脚本,所以我希望它对你有用。
我从来没有在Finder中使用过label index
属性,我通过一些测试发现我无法看到标签,直到我点击脚本后的文件夹运行.不过,在我这样做之后,所有目标文件都有正确的标签。
我以前从未使用过 AppleScript,所以我对这门语言很陌生,但我正在尽力而为。
这是我想要完成的: 运行 选择一个包含 .ARW 和 .JPG 文件的文件夹时的脚本。遍历文件夹中的项目。如果当前项目是 .ARW,则再次从头开始遍历文件夹。如果此嵌套迭代落在具有相同文件名和 JPG 扩展名的文件上,则将原始 ARW 文件标记为红色。
TLDR:如果文件夹中的 ARW 文件与文件夹中的 JPG 文件共享相同的文件名,则以红色突出显示 ARW 文件,否则什么都不做。
这是我到目前为止编写的代码:
tell application "Finder"
set totalAlias to the entire contents of (selection as alias)
set totalCount to the count of items of totalAlias
set firstName to name of item 1 of totalAlias
set firstExtension to name extension of item 1 of totalAlias
set c to 1
repeat while c ≤ totalCount
set currentAlias to item c of totalAlias
set currentName to name of currentAlias
set currentExtension to name extension of currentAlias
if currentExtension is "ARW" then
set d to 1
set compareFile to currentAlias
set findName to currentName
set findExtension to currentExtension
repeat while d ≤ totalCount
if (name of item d of totalAlias = findName) and (name extension of item d of totalAlias is "JPG") then
tell application "Finder" to set label index of compareFile to 2
end if
set d to (d + 1)
end repeat
end if
set c to (c + 1)
end repeat
end tell
有什么问题吗?我相信这与我的 IF AND 条件有关。
试试这个脚本:
tell application "Finder"
--Just get all the filenames of the target types:
set allJPG to the name of every file of (entire contents of (selection as alias)) whose name extension = "JPG"
set allARW to the name of every file of (entire contents of (selection as alias)) whose name extension = "ARW"
--Send the two lists to a handler to find all the common names
set targetJPGFiles to my CompareNames(allJPG, allARW)
--Loop through the common names, find the files, set the tags
repeat with eachTarget in targetJPGFiles
set fileToTag to (item 1 of (get every file of (entire contents of (selection as alias)) whose name is (eachTarget as text)))
set label index of fileToTag to 2
end repeat
end tell
targetJPGFiles -- This allows you to see the filenames that SHOULD have been tagged
to CompareNames(jp, pn)
--First, get rid of all the extensions in the ARW files
set cleanARWNames to {}
set neededJPGNames to {}
repeat with eachARWName in pn
set end of cleanARWNames to characters 1 thru -5 of (eachARWName as text) as text
end repeat
--Now, loop through JPG names to find a match
repeat with eachjpgName in jp
set searchName to characters 1 thru -5 of (eachjpgName as text) as text
if cleanARWNames contains searchName then
set end of neededJPGNames to (eachjpgName as text)
end if
end repeat
return neededJPGNames
end CompareNames
它采用了一种略有不同的方法,因为它只比较两个文件名列表,然后返回,找到具有所需名称的文件,并进行标记。
它基于我为另一个项目编写的脚本,所以我希望它对你有用。
我从来没有在Finder中使用过label index
属性,我通过一些测试发现我无法看到标签,直到我点击脚本后的文件夹运行.不过,在我这样做之后,所有目标文件都有正确的标签。