Ваш сценарий не компилируется, поэтому он не позволил мне протестировать его, чтобы действительно почувствовать, что вы делаете, поэтому я надеюсь, что я нахожусь в шаре с тем, что я придумал ниже .
Но, кроме этого, я буду тупым: ваш сценарий - беспорядок. У вас есть блок Finder , который содержит некоторый код AppleScriptObj C, а затем некоторые вызовы функций оболочки ... Я думаю, вам нужно выбрать one , а затем упорядочить код немного более логично, чтобы люди (особенно вы) могли понять, что происходит.
Я обычно избегаю Finder для операций с файловой системой, но в этой ситуации это выгодно, потому что это позволяет один, чтобы сравнить потенциальный список элементов (сгенерированный фильтром whose
) с известным списком элементов - что-то, что никакое другое приложение не позволяет (и вместо этого думает, что вы sh сравните его с числом 64
). Это также означает, что операция перемещения может быть отменена , если необходимо:
-- Bits of text for joining
-- Used for the alert dialogs
property lf : linefeed
property lft : linefeed & tab
property lf2 : lf & lf
property lf2t : lf2 & tab
property bullet : "›"
property li : bullet & space
-- Icon files, also used
-- for the alert dialogs
property CoreTypes : "/System/Library/CoreServices/CoreTypes.bundle"
property StopIcon : path to resource "AlertStopIcon.icns" in bundle CoreTypes
property NoteIcon : path to resource "AlertNoteIcon.icns" in bundle CoreTypes
on replace:sender
set prompt to "Please select some files:"
set fs to choose file with prompt prompt ¬
with multiple selections allowed
-- Get the directory in which the chosen files lie
set dir to (some item in fs & "::" as text)
set the text item delimiters to dir's POSIX path
-- Organise the files into two lists:
-- PNG files and non-PNG files
set PNGfs to {}
repeat with f in fs
get text items 2 thru -1 of f's POSIX path as text
set f's contents to the result
tell f to if its contents ends with ".png" then
set end of PNGfs to its contents
set its contents to null
end if
end repeat
set fs to every string in fs
set the text item delimiters to lft & li
if fs ≠ {} then display dialog ["The following files ", ¬
"are not PNG files and will be ignored:", lf2t, ¬
li & fs] as text with title ["Oops…!"] buttons ¬
["D'oh!"] with icon NoteIcon default button 1
if PNGfs = {} then return -- Nothing to move
tell application id "com.apple.Finder"
set here to dir as alias -- The source folder
set there to the (path to the documents folder) -- Destination folder
-- Enumerate files that might be replaced
set duplicates to the name of every file ¬
in there whose name is in PNGfs
if duplicates ≠ {} then tell (display dialog contents ¬
of ["The following files in ", here's POSIX path, ¬
" share names with files in ", there's POSIX path, ¬
":", lf2t & li & duplicates & lf2, "Do you want ", ¬
"to:", lft, "• Move all files anyway, replacing ", ¬
"the ones in ", there's POSIX path, ";", lft, "•", ¬
" Move only the files that can be moved without ", ¬
"replacing anything; OR", lft, "• Don't move any", ¬
" of the files for now ?"] as text ¬
with title ["Replace Existing Files?"] ¬
buttons ["Replace", "Keep", "Abort"] ¬
default button 1 with icon StopIcon) ¬
to set do to its button returned
-- If the user aborts, the script terminates.
-- If the user elects to replace existing files,
-- then we move those existing files to the trash.
-- If the user wishes to keep the existing files,
-- they remain in place. Either way, the final
-- operation is the same: move the files without
-- replacing anything.
if do = "Abort" then return 0 -- No files moved
if do = "Replace" then delete ¬
(files in there whose ¬
name is in ¬
duplicates)
move the (files in here whose ¬
name is in PNGfs) to ¬
there without replacing
end tell
end replace:
Делая это таким образом, вы избегаете повторения l oop, и, таким образом, вы получаете только одно диалоговое окно на набор. сгруппированных событий (одно, если пользователь выбирает файлы неправильного типа; другое, если существует риск перезаписи файлов).
На самом деле, вы даже можете избавиться от первого повтора l oop, то есть используется для разделения списка на два типа файлов: команда choose file
имеет параметр с именем of type
, где вы можете указать один или несколько типов файлов, которые будут ограничены выбором пользователя:
set fs to choose file with prompt prompt ¬
with multiple selections allowed ¬
of type ["png"] --OR:["public.png"]
"public.png"
- это универсальный идентификатор типа для файлов PNG.