如何使用AppleScript为包含图像的文件夹生成html img标签?

时间:2014-04-12 09:26:40

标签: applescript

我试图找到如何使用Automator或AppleScript从图像目录生成html <img>标记,包括图像宽度和高度尺寸。

要做到这一点,我需要:

  1. 选择多个文件,仅限于图片
  2. 制作一个遍历每个文件的循环
  3. 查找图片尺寸
  4. 构建/连接字符串

1 个答案:

答案 0 :(得分:1)

我从来没有做过AppleScript,但将它拼凑在一起,主要归功于image events reference。建议欢迎。

-- select multiple files, limited to images
set filelist to choose file of type "public.image" with multiple selections allowed

set html to ""

-- make a loop that goes through each file
repeat with imagefile in filelist
    tell application "Image Events"
        launch
        set img to open imagefile
        -- get dimensions of image
        copy the dimensions of img to {W, H}
        -- build / concatenate html string
        set html to html & "<img alt=\"\" src=\"" & name of imagefile & "\" width=\"" & W & "\" height=\"" & H & "\" />
"
        close img
    end tell
end repeat

set the clipboard to html
display dialog "html for " & length of filelist & " images copied to the clipboard!"

html