我想在 applescript droplet 中设置一次变量,但它让我为每个文件设置它

I want to set variable once in an applescript droplet but it makes me set it for every file

我写了一个 applescript droplet,我想:

  1. 将一张或多张图片拖到 droplet 上
  2. 出现 3 个显示对话框询问 'width'、'height'、'format'
  3. 使用返回的上述文本处理所有丢弃的图像 3 个显示对话框

目前,每张图片都会显示 3 个显示对话框(例如,显示 3 张图片 = 9 个对话框)。有没有办法让我只能回答一次这些对话?这是我的脚本:

on run
    display dialog "This is a droplet"
end run

on open draggedItems

set tid to AppleScript's text item delimiters

--ask for new width
set newWidth to text returned of (display dialog "New Width" default answer ¬
    "45" buttons {"Continue…", "Cancel"} ¬
    default button 1)

--ask for new height
set newHeight to text returned of (display dialog "New Height" default answer ¬
    "45" buttons {"Continue…", "Cancel"} ¬
    default button 1)

--ask for formatType
set newFormat to text returned of (display dialog "Image Format" default answer ¬
    "jpg" buttons {"Continue…", "Cancel"} ¬
    default button 1)
--repeat
repeat with i in draggedItems
    set theFile to (i as alias)
    set theFilePath to (the POSIX path of theFile)
    set fullFileName to name of (info for theFile without size)
    set AppleScript's text item delimiters to "."
    set fileNameNoExtension to first text item of fullFileName
    --set fileExtension to second text item of fullFileName
    set AppleScript's text item delimiters to tid

    do shell script ("/usr/local/bin/convert " & quoted form of theFilePath & " -resize " & newWidth & "x" & newHeight & "\! ~/desktop/" & fileNameNoExtension & "." & newFormat)
end repeat
end open

on open draggedItems 已经是一个重复循环,如果你在这个 droplet

上放置 4 个文件
on open draggedItems
    display dialog (draggedItems as text)
end open

您将获得 4 个不同的对话框。

你可以这样做

property askingForNew : true

on open draggedItems
    if askingForNew is true then
        --display dialogs
        set askingForNew to false
    end if
    display dialog (draggedItems as text)
end open

或将文件夹拖到 droplet 上并浏览其中的文件 (like this)。