尝试做一些我认为很容易的事情,那就是使用javascript获取在线图像的尺寸,然后将这些值返回给AppleScript。
请查看此源代码,我认为它可以正常工作,但是当涉及Javascript时,我是一个完全菜鸟。
set theVar to "https://i.ebayimg.com/images/g/M58AAMXQaBtRAPkA/s-l500.jpg"
set theScript to "var img = theVar;
var height = img.height;
var width = img.width;"
tell application "Safari"
do JavaScript theScript
end tell
set theResult to result
我希望将尺寸作为结果返回。
答案 0 :(得分:1)
这不是JavaScript解决方案。但是,下面的AppleScript代码将从您在代码的第1行中定义的图像URL下载所有图像。接下来,它将写入文件,图像名称及其尺寸。该代码的末尾提供了一个删除已下载图像的命令,并将在Finder中显示包含图像名称及其尺寸的文本文件。
文件夹和文件路径未经过硬编码,因此您所要做的就是在此AppleScript第一行中输入变量的图像URL。
此AppleScript代码适用于使用最新版本的macOS Mojave的我。
set imageURL to {"https://i.ebayimg.com/images/g/M58AAMXQaBtRAPkA/s-l500.jpg", "https://i.imgur.com/gQQHPBn.png"} -- Can Enter Multiples
-- Downloads The Images (Defined Above) To The Folder (Defined Below)
set downloadedImagesFolder to ((path to downloads folder as text) & "Images For Dimensions") as text
set quotedFormOfDownloadedImagesFolder to quoted form of POSIX path of downloadedImagesFolder
-- Writes Name Of Image And It's Dimensions To The File (Defined Below)
set imageDimensionsFile to ((path to downloads folder as text) & "File_Dimensions.txt") as text
set quotedFormOfImageDimensionsFile to quoted form of POSIX path of imageDimensionsFile
tell application "Finder"
if not (exists of folder downloadedImagesFolder) then
make new folder at (path to downloads folder) ¬
with properties {name:"Images For Dimensions"}
end if
end tell
repeat with i in imageURL
set imageURLText to i
set text item delimiters to "https" -- shell script "lwp-download " errors with "https"
set tempURL to text items of imageURLText
set text item delimiters to "http" -- replaces "https" with "http"
set finalImgeURL to tempURL as text
set text item delimiters to ""
try
do shell script "lwp-download " & quoted form of finalImgeURL & " " & quotedFormOfDownloadedImagesFolder
end try
end repeat
tell application "Finder"
set imageFiles to files of folder downloadedImagesFolder as alias list
set quotedFormOfImageFiles to {}
repeat with i in imageFiles
set end of quotedFormOfImageFiles to quoted form of POSIX path of i
end repeat
end tell
repeat with thisItem in quotedFormOfImageFiles
delay 2 -- May Need To Increase Value If Returning Null Value Results
-- replace " >> " with " > " Below... If You Prefer To Overwrite The File Instead Of Append
do shell script "mdls -name kMDItemFSName -name kMDItemPixelHeight -name kMDItemPixelWidth " & thisItem & " >> " & quotedFormOfImageDimensionsFile
end repeat
tell application "Finder" to reveal imageDimensionsFile
(* Comment Out Or Remove The Following Line If You Don't Want To
Delete The Downloaded Images *)
tell application "Finder" to delete folder downloadedImagesFolder
答案 1 :(得分:1)
需要下载文件才能使用图像,但是不一定需要保存它们。您可以使用 AppleScript 或 JXA 将URL的内容获取到 NSImage 中,并在其中获取其size
属性-例如:
AppleScript:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
set theURL to current application's NSURL's URLWithString:"https://i.ebayimg.com/images/g/M58AAMXQaBtRAPkA/s-l500.jpg"
set theImage to current application's NSImage's alloc's initByReferencingURL:theURL
theImage's |size| as record
编辑:
再看看注释,如果使用的URL不是图像,则使用initWithContentsOfURL
时两个脚本编辑器都会崩溃(osascript
也会出错)。使用initByReferencingURL
似乎可以正常工作(您也可以使用valid
属性来检查图像)。
JXA:
ObjC.import('Cocoa')
theURL = $.NSURL.URLWithString('https://i.ebayimg.com/images/g/M58AAMXQaBtRAPkA/s-l500.jpg')
theImage = $.NSImage.alloc.initWithContentsOfURL(theURL)
theImage.size
答案 2 :(得分:1)
鉴于您问题中的网址只有一张图片,请考虑以下要点:
set theURL to "https://i.ebayimg.com/images/g/M58AAMXQaBtRAPkA/s-l500.jpg"
set js to "[].slice.call(document.querySelectorAll('img')).map(function(img) {return {src: img.src, width: img.width, height: img.height}});"
tell application "Safari"
make new document with properties {URL:theURL}
delay 4
set imgProps to (do JavaScript js in current tab of window 1)
if (length of imgProps > 0) then
set {width:width} to item 1 of imgProps
set {height:height} to item 1 of imgProps
set {src:src} to item 1 of imgProps
log width
log height
log src
end if
end tell
说明:
分配给名为js
的AppleScript变量的JavaScript的以下行:
[].slice.call(document.querySelectorAll('img')).map(function(img) {return {src: img.src, width: img.width, height: img.height}});
document
的{{3}}方法来获取DOM中每个NodeList
元素的img
。 [].slice.call(...)
部分将NodeList
(类似于 array )转换为Array-这使我们能够利用Array提供的方法,例如querySelectorAll()
。
注意:对于支持map()
的现代版Safari,您可以使用ecmascript-6方法代替。但是,[].slice.call(...)
适用于仅支持ES5和现代ES6的Safari版本。
提供给map
的回调函数,即该部分;
function(img) {return {src: img.src, width: img.width, height: img.height}}
return
是一个对象,其中每个src
元素的属性为width
,height
和img
。
make new document with properties {URL:theURL}
部分在Safari中使用给定的URL创建了一个新文档。它实际上是打开/加载URL。
delay 4
部分利用Array.from(...)
命令暂停脚本执行(在此示例中为4秒),以使网页有时间加载,然后继续执行JavaScript。>
重要,您可能需要增加/减少此持续时间,具体取决于页面加载的速度。您还可以考虑尝试其他等待网页加载的解决方案,例如delay
和this post。实质上,Safari的AppleScript API并未为此提供内置功能,因此任何解决方案,包括使用delay
命令,都是一种解决方法/破解¯\_(ツ)_/¯
set imgProps to do JavaScript js in current tab of window 1
部分执行JavaScript并将结果(即this post的AppleScript list)分配给imgProps
变量。 / p>
最后一部分:
set {width:width} to item 1 of imgProps
set {height:height} to item 1 of imgProps
set {src:src} to item 1 of imgProps
log width
log height
log src
将width
中第一个height
的每个属性(src
,record
和list
的值分配给它们自己的变量; width
,height
和src
。
最后,我们记录每个变量的值。
注意这些最后的部分位于if (length of imgProps > 0) then
语句的正文中,以便仅在给定网页包含一个或多个图像时才运行。
可以使用与上述相同的Javascript代码来获取网页中所有img
元素的属性。以下示例中的显着区别是:
img
元素。delay
的持续时间(注意:根据需要更改此时间)。record
和list
属性中的每个log
。set theURL to "https://en.wikipedia.org/wiki/Car"
set js to "[].slice.call(document.querySelectorAll('img')).map(function(img) {return {src: img.src, width: img.width, height: img.height}});"
tell application "Safari"
make new document with properties {URL:theURL}
delay 8
set imgProps to do JavaScript js in current tab of window 1
if (length of imgProps > 0) then
repeat with img in items of imgProps
log width of img
log height of img
log src of img
log "-------"
end repeat
end if
end tell
答案 3 :(得分:0)
您可以尝试一种hacky方法:
set theURL to "https://i.ebayimg.com/images/g/M58AAMXQaBtRAPkA/s-l500.jpg"
tell application "Safari"
set URL of document 1 to theURL
delay 1 --> lazy way to let image load; also see below
tell window 1
tell tab 1
do JavaScript ("window.location.reload();") -- refresh window; otherwise you might get "Untitled"
end tell
end tell
return name of document 1 --> "s-l500.jpg 500×301 pixels" -- from here you can parse to get dimensions
end tell