Несколько, если условия внутри вложенного повторяются, пока в 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

Есть мысли о том, что идет не так? Я полагаю, что это связано с моим условием "ЕСЛИ И".

1 ответ

Решение

Попробуйте этот скрипт:

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

Он использует немного другой подход, так как он сравнивает только два списка имен файлов, затем возвращается, находит файлы с нужными именами и делает тегирование.

Он основан на сценарии, который я написал для другого проекта, поэтому я надеюсь, что он работает для вас.

Я никогда не использовал label index свойства в Finder и раньше, и в ходе некоторых испытаний я обнаружил, что не вижу метки, пока не нажму на папку после запуска скрипта. Все целевые файлы имели правильный тег после того, как я это сделал.

Другие вопросы по тегам