Applescript 将图像从我桌面上的文件夹复制到 excel 列 A
Applescript to copy images from a folder on my desktop to excel column A
问题总结:
我想创建一个 Applescript 以将图像从我桌面上的文件夹粘贴到 excel 列。有数千张图片,因此手动将照片添加到 excel 是行不通的。
假设文件夹中有 500 张图片,标题为:
Image_1.jpg... Image_2.jpg.. ... Image_500.jpg
我希望这些实际图像在 B 列的 excel 文件中
看起来像这样:
Column A Column B
Image_1.jpg Actual image of Image_1.jpg
Image_2.jpg Actual image of Image_2.jpg
... ...
Image_500.jpg Actual image of Image_500.jpg
我不需要任何花哨的东西来计算一个文件夹中有多少张图片或其他任何东西,但我只是不知道如何告诉 excel 粘贴图片 1,然后向下移动一行,粘贴第二个等等。也应该以单元格为中心。
我可以手动更改行数.. 所以如果我有 382 张图像或 1382 张图像,我可以手动将其输入到该特定文件夹的脚本中,并在下次我更改数量 运行 如果它的照片数量不同。
我基本上是在 excel 上做一个规格 sheet,我可以在其中做标题,在下一栏中,它的实际照片。
提供背景,包括您已经尝试过的内容
我已经在 filePath、imagefolder、Imagelist 上尝试了一些选项,但 none 有效。
显示一些代码
到目前为止,唯一可行的是粘贴一张单独的图像。我打算这样做,然后复制粘贴同一行,将其从 Image_1.jpg 更改为 2,3,4,...500... 但可能太耗时了。
此方法也仅将照片插入为小方形样本而不是清晰的图像。
set theFilePath to "Macintosh HD:Users:Christian:Desktop:ImageFolder:Image_1.jpg"
tell application "Microsoft Excel"
tell active sheet
make new picture at end with properties {file name:theFilePath}
end tell
end tell
描述预期和实际结果,包括任何错误消息
我不断遇到的错误是:
Microsoft Excel 出现错误:无法制作 class 图片。”数字 -2710 从图片到 class
此脚本或多或少应该可以解决问题。
set imageFolder to POSIX path of (choose folder)
tell application "System Events"
set imageList to POSIX path of files of folder imageFolder whose visible is true
end tell
set targetImageHeight to 140
set largestImageWidth to 0
set imageCount to count of imageList
set idx to 1
repeat with imageFile in imageList
set targetImageWidth to widthOfPic(imageFile, targetImageHeight)
tell application "Microsoft Excel"
activate
set nameCell to cell ("A" & idx as string) of worksheet 1 of workbook 1
set value of nameCell to my imageName(imageFile)
set imageCell to cell ("B" & idx as string) of worksheet 1 of workbook 1
set theLeft to left position of imageCell
set theTop to (top of imageCell)
set newPic to make new picture at beginning of worksheet 1 of workbook 1 with properties {file name:POSIX file imageFile, height:targetImageHeight, width:targetImageWidth, top:theTop, left position:theLeft, placement:placement move}
set row height of imageCell to targetImageHeight
if targetImageWidth > largestImageWidth then
set largestImageWidth to targetImageWidth
-- this next line is purely cosmetic, to make it look
-- like the image is in the cell
set column width of imageCell to largestImageWidth * 0.1625
end if
end tell
set idx to idx + 1
end repeat
tell application "Microsoft Excel"
tell worksheet 1 of workbook 1
tell column "A:A"
autofit
set vertical alignment to vertical alignment center
end tell
end tell
end tell
on imageName(imagePath)
tell application "System Events"
return name of file imagePath
end tell
end imageName
on widthOfPic(picPath, targetH)
set sipsOutput to do shell script "sips -g pixelHeight -g pixelWidth " & quoted form of picPath
set h to (last word of paragraph 2 of sipsOutput) as integer
set w to (last word of paragraph 3 of sipsOutput) as integer
return targetH * w / h
end widthOfPic
问题总结:
我想创建一个 Applescript 以将图像从我桌面上的文件夹粘贴到 excel 列。有数千张图片,因此手动将照片添加到 excel 是行不通的。
假设文件夹中有 500 张图片,标题为: Image_1.jpg... Image_2.jpg.. ... Image_500.jpg
我希望这些实际图像在 B 列的 excel 文件中
看起来像这样:
Column A Column B
Image_1.jpg Actual image of Image_1.jpg
Image_2.jpg Actual image of Image_2.jpg
... ...
Image_500.jpg Actual image of Image_500.jpg
我不需要任何花哨的东西来计算一个文件夹中有多少张图片或其他任何东西,但我只是不知道如何告诉 excel 粘贴图片 1,然后向下移动一行,粘贴第二个等等。也应该以单元格为中心。
我可以手动更改行数.. 所以如果我有 382 张图像或 1382 张图像,我可以手动将其输入到该特定文件夹的脚本中,并在下次我更改数量 运行 如果它的照片数量不同。
我基本上是在 excel 上做一个规格 sheet,我可以在其中做标题,在下一栏中,它的实际照片。
提供背景,包括您已经尝试过的内容
我已经在 filePath、imagefolder、Imagelist 上尝试了一些选项,但 none 有效。
显示一些代码
到目前为止,唯一可行的是粘贴一张单独的图像。我打算这样做,然后复制粘贴同一行,将其从 Image_1.jpg 更改为 2,3,4,...500... 但可能太耗时了。
此方法也仅将照片插入为小方形样本而不是清晰的图像。
set theFilePath to "Macintosh HD:Users:Christian:Desktop:ImageFolder:Image_1.jpg"
tell application "Microsoft Excel"
tell active sheet
make new picture at end with properties {file name:theFilePath}
end tell
end tell
描述预期和实际结果,包括任何错误消息
我不断遇到的错误是:
Microsoft Excel 出现错误:无法制作 class 图片。”数字 -2710 从图片到 class
此脚本或多或少应该可以解决问题。
set imageFolder to POSIX path of (choose folder)
tell application "System Events"
set imageList to POSIX path of files of folder imageFolder whose visible is true
end tell
set targetImageHeight to 140
set largestImageWidth to 0
set imageCount to count of imageList
set idx to 1
repeat with imageFile in imageList
set targetImageWidth to widthOfPic(imageFile, targetImageHeight)
tell application "Microsoft Excel"
activate
set nameCell to cell ("A" & idx as string) of worksheet 1 of workbook 1
set value of nameCell to my imageName(imageFile)
set imageCell to cell ("B" & idx as string) of worksheet 1 of workbook 1
set theLeft to left position of imageCell
set theTop to (top of imageCell)
set newPic to make new picture at beginning of worksheet 1 of workbook 1 with properties {file name:POSIX file imageFile, height:targetImageHeight, width:targetImageWidth, top:theTop, left position:theLeft, placement:placement move}
set row height of imageCell to targetImageHeight
if targetImageWidth > largestImageWidth then
set largestImageWidth to targetImageWidth
-- this next line is purely cosmetic, to make it look
-- like the image is in the cell
set column width of imageCell to largestImageWidth * 0.1625
end if
end tell
set idx to idx + 1
end repeat
tell application "Microsoft Excel"
tell worksheet 1 of workbook 1
tell column "A:A"
autofit
set vertical alignment to vertical alignment center
end tell
end tell
end tell
on imageName(imagePath)
tell application "System Events"
return name of file imagePath
end tell
end imageName
on widthOfPic(picPath, targetH)
set sipsOutput to do shell script "sips -g pixelHeight -g pixelWidth " & quoted form of picPath
set h to (last word of paragraph 2 of sipsOutput) as integer
set w to (last word of paragraph 3 of sipsOutput) as integer
return targetH * w / h
end widthOfPic