Обработчик сценариев Apple не работает в операторе Tell
Привет, я пишу скрипт, который переименует выбранные файлы / папки в формате xxxyearxxx в xxxyear. Я новичок в сценарии Apple, сделал много вещей autoit / C++.
Я не могу заставить своего обработчика работать с оператором Tell. Я перепробовал все ("мое", "меня") и получил ошибки с обработчиком. Обработчик работает нормально, когда вызывается вне оператора Tell:
с "my", сценарий не компилируется, и я получаю следующую ошибку: Синтаксическая ошибка: имя команды не может идти после этого "my".
with "of me" Этот скрипт компилируется и запускается, но я получаю следующую ошибку: "Finder получил ошибку: отсутствует параметр using для splittext." номер -1701 от "класс по"
Любая помощь, чтобы обойти это было бы очень полезно. Сценарий ниже:
`
set MaxYear to 2018
set MinYear to 1990
-- return splittext {"abc2015def", 2018, 1990}
tell application "Finder"
set allfiles to every item of (choose folder with prompt "Choose the Files you'd like to rename:" with multiple selections allowed) as list
-- set allfile to selections
repeat with x in allfiles
if kind of x is "Folder" then
-- if xtype is "Folder" then
set cname to name of x
set newname to splittext {cname, MaxYear, MinYear} of me
if newname then
set name of x to newname
end if
end if
end repeat
end tell
on splittext {theText, MaxYear, MinYear}
set dyear to MaxYear
repeat until dyear < MinYear
set AppleScript's text item delimiters to dyear
set theTextItems to every text item of theText
set AppleScript's text item delimiters to ""
if (count of theTextItems) > 1 then
return the first item of theTextItems & dyear as string
end if
set dyear to dyear - 1
end repeat
return false
end splittext
2 ответа
Благодаря этому, в конце концов я обнаружил, что ошибка splittext является своего рода внутренним элементом AppleScript. Я не добавлял фигурные скобки, программа автоматически заменяла мои стандартные скобки на фигурные скобки.
Как только я изменил название на _splittext, все работало нормально.
Этот код работает, но...
ваш обработчик:
splittext {theText, MaxYear, MinYear}
должно быть:
splitText(theText, MaxYear, MinYear}
Обратите внимание на круглые скобки вместо фигурных скобок (и верблюжья оболочка тоже).
Вы всегда должны возвращать строку в вашем обработчике:
return "" -- return false
И в цикле повтора выше:
if newname is not "" then -- if newname then
Есть и другие подводные камни: вид х. Это локальная строка, т.е. "Папка" будет "Досье" на французском языке. Возможно, вы должны сделать что-то вроде этого, если вы не из англоговорящей страны:
log kind of x
Итак, весь код, который работает на моей машине (10.12.6):
set MaxYear to 2018
set MinYear to 1990
--return splitText("abc2015def", 2018, 1990)
tell application "Finder"
set allfiles to every item of (choose folder with prompt "Choose the Files you'd like to rename:" with multiple selections allowed) as list
-- set allfile to selections
repeat with x in allfiles
log kind of x
if kind of x is "Folder" then
-- if xtype is "Folder" then
set cname to name of x
set newname to my splitText(cname, MaxYear, MinYear)
if newname is not "" then
set name of x to newname
end if
end if
end repeat
end tell
on splitText(theText, MaxYear, MinYear)
set dyear to MaxYear
repeat until dyear < MinYear
set AppleScript's text item delimiters to dyear
set theTextItems to every text item of theText
set AppleScript's text item delimiters to ""
if (count of theTextItems) > 1 then
return the first item of theTextItems & dyear as string
end if
set dyear to dyear - 1
end repeat
return ""
end splitText