- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Mac 上,是否可以在 Automator 应用程序运行时使用 AppleScript/Automator 在后台显示图像预览?
我有数百张图像,我想向其中添加元数据,以便它们可以显示在网站上。我的计划是创建一个与原始图像同名的 *.meta
文件,这样 PHP 脚本就可以在为图像生成 URL 的同时读取元数据。
我创建了一个 AppleScript 文件(见下文),并将其嵌入到 Automator 应用程序中。当您将选择的文件拖放到应用程序上时,它首先会显示图像,然后显示 3 个对话框,您可以在其中输入所需的数据。
问题是 AppleScript 在 qlmanage 预览窗口打开时被阻止。在输入所需数据之前,您需要关闭窗口,这样您就看不到图像了。
有没有办法将 qlmanage 进程发送到后台,以便在图像打开时可以显示对话窗口?
或者也许已经有一款适用于 Mac OS 的免费工具可以让您完全按照我的意愿去做。
tell application "Finder"
set the_selection to the selection
end tell
set file_types to {"jpg", "png", "gif", "mp4"}
set copyright to "© 2015 CompanyName"
set signature to "Signature"
repeat with ii from 1 to the count of the_selection
set {posix_path, file_path, file_name, file_ext} to splitPath(item ii of the_selection)
if file_types contains file_ext then
set meta_path to file_path & file_name & ".meta"
tell application "Finder"
if not (exists file (meta_path)) then
do shell script "qlmanage -t -s 640 " & posix_path
set alt_text to the text returned of (display dialog "alt" default answer file_name)
set copyright to the text returned of (display dialog "©" default answer copyright)
set signature to the text returned of (display dialog "signature" default answer signature)
set meta_text to "alt " & alt_text & return & "copyright " & copyright & return & "signature " & signature
set meta_file to open for access meta_path with write permission
write meta_text to meta_file
close access meta_file
end if
end tell
end if
end repeat
on splitPath(selected_item)
tell application "System Events"
set posix_path to POSIX path of (selected_item as alias)
set posix_path to quoted form of posix_path
end tell
set file_path to selected_item as string
set file_ext to ""
set file_name to ""
set text item delimiters to "."
if (count text items of file_path) > 1 then
set file_ext to last text item of file_path
set file_path to (text items 1 through -2 of file_path) as string
end if
set text item delimiters to ":"
if (count text items of file_path) > 1 then
set file_name to last text item of file_path
set file_path to ((text items 1 through -2 of file_path) as string) & ":"
end if
return {posix_path, file_path, file_name, file_ext}
end splitPath
最佳答案
我编写了一个应用程序,它完成了您在 Script Editor.app 中使用 ApplescriptOBJc 所做的大部分工作(主要是为了看看它有多容易(它曾经是))
ApplescriptOBJc 是 Applescript 和 Objective - c 之间的语法桥接语言。
这种语言允许您访问 Objective-c 的强大功能,并在同一代码中将其与 Applescript 一起使用。
我唯一没有添加的是检查视频文件。
但由于这是一个示例/入门代码,您应该能够很容易地做到这一点。
我通常也会使用 ApplescriptOBJc 来写出文件,但为了熟悉而使用了您的代码
代码应该足够简单,可以遵循、更改、添加或移动 UI 对象,还可以更改或更新其逻辑。
将代码粘贴到新的脚本编辑器文档中。然后将其保存为保持打开状态的应用程序。
应用代码:
-- Copyright 2015 {Mark Hunte}. All rights reserved.
use scripting additions
use framework "Foundation"
use framework "cocoa"
use framework "AppKit"
--- set up window
property buttonWindow : class "NSWindow"
property theImage : class "NSImage"
property theImageView : class "NSImageView"
property altLabelField : class "NSTextField"
property altField : class "NSTextField"
property copyrightField : class "NSTextField"
property sigField : class "NSTextField"
property countLabelField : class "NSTextField"
property the_selection : {}
property imageList : {} -- holds NSImage instances
property imageListSplit : {} -- to use for file when needing an alias
property thisImage : 0
property imageCount : 0
property copyright : "© 2015 CompanyName"
property signature : "Signature"
set height to 700
set width to 1000
set winRect to current application's NSMakeRect(0, 0, width, height)
set buttonWindow to current application's NSWindow's alloc()'s initWithContentRect:winRect styleMask:7 backing:2 defer:false
buttonWindow's setFrameAutosaveName:"buttonWindow"
--set up buttons
set writeButtonFrame to current application's NSMakeRect(105, (height - 230), 100, 25) -- button rect origin ,x,y ,size width,hieght
set writeBtn to current application's NSButton's alloc's initWithFrame:writeButtonFrame -- init button
writeBtn's setTitle:"write file"
set writeBtn's bezelStyle to 12 --NSRoundedBezelStyle
writeBtn's setButtonType:0 --NSMomentaryLightButton
writeBtn's setTarget:me
writeBtn's setAction:"writeFile:"
set nextButtonFrame to current application's NSMakeRect(105, (height - 675), 50, 25) -- button rect origin ,x,y ,size width,hieght
set nextBtn to current application's NSButton's alloc's initWithFrame:nextButtonFrame -- init button
nextBtn's setTitle:"Next"
set nextBtn's bezelStyle to 12 --NSRoundedBezelStyle
nextBtn's setButtonType:0 --NSMomentaryLightButton
nextBtn's setTarget:me
nextBtn's setAction:"nextImage:"
--
set prevButtonFrame to current application's NSMakeRect(25, (height - 675), 50, 25)
set prevBtn to current application's NSButton's alloc's initWithFrame:prevButtonFrame
prevBtn's setTitle:"Prev"
set prevBtn's bezelStyle to 12 --NSRoundedBezelStyle
prevBtn's setButtonType:0 --NSMomentaryLightButton
prevBtn's setTarget:me
prevBtn's setAction:"previousImage:"
---
set selectionButtonFrame to current application's NSMakeRect((width - 715), (height - 690), 150, 25)
set selectionBtn to current application's NSButton's alloc's initWithFrame:selectionButtonFrame
selectionBtn's setTitle:"Select new Images"
set selectionBtn's bezelStyle to 12 --NSRoundedBezelStyle
selectionBtn's setButtonType:0 --NSMomentaryLightButton
selectionBtn's setTarget:me
selectionBtn's setAction:"finderSelection:"
--
set terminatButtonFrame to current application's NSMakeRect((width - 90), (height - 690), 75, 25)
set terminateBtn to current application's NSButton's alloc's initWithFrame:terminatButtonFrame
terminateBtn's setTitle:"Quit"
set terminateBtn's bezelStyle to 12 --NSRoundedBezelStyle
terminateBtn's setButtonType:0 --NSMomentaryLightButton
terminateBtn's setTarget:me
terminateBtn's setAction:"terminateMe"
--
-- add buttons to the window
buttonWindow's contentView's addSubview:nextBtn
buttonWindow's contentView's addSubview:prevBtn
buttonWindow's contentView's addSubview:selectionBtn
buttonWindow's contentView's addSubview:terminateBtn
buttonWindow's contentView's addSubview:writeBtn
--
-- set up image view
set imageViewFrame to current application's NSMakeRect(300, (height - 660), 640, 640) --origin ,x,y ,size width,hieght
set theImageView to current application's NSImageView's alloc()'s initWithFrame:imageViewFrame
theImageView's setImageFrameStyle:1
-- add image view to the window
buttonWindow's contentView's addSubview:theImageView
-- activate the window
buttonWindow's makeKeyAndOrderFront:buttonWindow
--- set alt label
set altLabelFrame to current application's NSMakeRect(25, (height - 60), 100, 25) --origin ,x,y ,size width,hieght
set altLabelField to current application's NSTextField's alloc()'s initWithFrame:altLabelFrame
altLabelField's setStringValue:"alt"
altLabelField's setBezeled:false
altLabelField's setDrawsBackground:false
altLabelField's setEditable:false
altLabelField's setSelectable:false
-- set up alt textField
set altFieldFrame to current application's NSMakeRect(25, (height - 80), 240, 25) --origin ,x,y ,size width,hiegh
set altField to current application's NSTextField's alloc()'s initWithFrame:altFieldFrame
---
--- set copyright label
set copyrightLabelFrame to current application's NSMakeRect(25, (height - 110), 100, 25) --origin ,x,y ,size width,hieght
set copyrightLabelField to current application's NSTextField's alloc()'s initWithFrame:copyrightLabelFrame
copyrightLabelField's setStringValue:"©opyright"
copyrightLabelField's setBezeled:false
copyrightLabelField's setDrawsBackground:false
copyrightLabelField's setEditable:false
copyrightLabelField's setSelectable:false
-- set up copyright textFields
set copyrightFieldFrame to current application's NSMakeRect(25, (height - 130), 240, 25) --origin ,x,y ,size width,hieght
set copyrightField to current application's NSTextField's alloc()'s initWithFrame:copyrightFieldFrame
--- set sig label
set sigLabelFrame to current application's NSMakeRect(25, (height - 160), 100, 25) --origin ,x,y ,size width,hieght
set sigLabelField to current application's NSTextField's alloc()'s initWithFrame:sigLabelFrame
sigLabelField's setStringValue:"signature"
sigLabelField's setBezeled:false
sigLabelField's setDrawsBackground:false
sigLabelField's setEditable:false
sigLabelField's setSelectable:false
sigLabelField's setDelegate:me
-- set up sig textFields
set sigFieldFrame to current application's NSMakeRect(25, (height - 180), 240, 25) --origin ,x,y ,size width,hieght
set sigField to current application's NSTextField's alloc()'s initWithFrame:sigFieldFrame
--- set image count label
set countLabelFrame to current application's NSMakeRect(500, (height - 25), 100, 25) --origin ,x,y ,size width,hieght
set countLabelField to current application's NSTextField's alloc()'s initWithFrame:countLabelFrame
countLabelField's setStringValue:"0"
countLabelField's setBezeled:false
countLabelField's setDrawsBackground:false
countLabelField's setEditable:false
countLabelField's setSelectable:false
--
buttonWindow's contentView's addSubview:altLabelField
buttonWindow's contentView's addSubview:altField
buttonWindow's contentView's addSubview:copyrightLabelField
buttonWindow's contentView's addSubview:copyrightField
buttonWindow's contentView's addSubview:sigLabelField
buttonWindow's contentView's addSubview:sigField
buttonWindow's contentView's addSubview:countLabelField
---
my finderSelection:(missing value)
---
on nextImage:sender
buttonWindow's makeFirstResponder:(missing value)
if thisImage is not greater than imageCount and thisImage is not equal to imageCount then
set thisImage to thisImage + 1
theImageView's setImage:(item thisImage of imageList)
end if
setUpTextFieldsStrings()
end nextImage:
on previousImage:sender
buttonWindow's makeFirstResponder:(missing value)
if thisImage is less than 1 or thisImage is not equal to 1 then
set thisImage to thisImage - 1
theImageView's setImage:(item thisImage of imageList)
end if
setUpTextFieldsStrings()
end previousImage:
on setUpTextFieldsStrings()
tell application "Finder" to set file_name to displayed name of ((item thisImage of imageListSplit) as alias)
set altField's stringValue to file_name
set copyrightField's stringValue to copyright
set sigField's stringValue to signature
set countLabelField's stringValue to (thisImage & " of " & (count of imageListSplit) as string)
end setUpTextFieldsStrings
on writeFile:sender
buttonWindow's makeFirstResponder:(missing value)
set {posix_path, file_path, file_name, file_ext} to splitPath(item thisImage of imageListSplit)
set meta_path to file_path & file_name & ".meta"
tell application "Finder"
if not (exists file (meta_path)) then
set alt_text to altField's stringValue
set copyrightText to copyrightField's stringValue
set signatureText to sigField's stringValue
set meta_text to "alt " & alt_text & return & "copyright " & copyrightText & return & "signature " & signatureText
set meta_file to open for access meta_path with write permission
write meta_text to meta_file
close access meta_file
end if
end tell
end writeFile:
on finderSelection:sender
buttonWindow's makeFirstResponder:(missing value)
set the_selection to {}
set imageList to {}
set imageListSplit to {}
set imageCount to 0
set thisImage to 0
using terms from application "Finder"
set the_selection to (choose file with prompt "Please select images:" with multiple selections allowed without invisibles)
end using terms from
repeat with i from 1 to number of items in the_selection
set this_item to (POSIX path of (item i of the_selection as alias))
set workSpace to current application's NSWorkspace's sharedWorkspace
set type to (workSpace's typeOfFile:this_item |error|:(missing value))
if (workSpace's type:type conformsToType:"public.image") then
set end of imageList to (current application's NSImage's alloc()'s initWithContentsOfFile:this_item)
set end of imageListSplit to item i of the_selection
end if
end repeat
if imageList is not {} then
set imageCount to count of imageList
theImageView's setImage:(item 1 of imageList)
set thisImage to 1
my setUpTextFieldsStrings()
end if
end finderSelection:
on terminateMe()
tell me to quit
end terminateMe
on splitPath(selected_item)
tell application "System Events"
set posix_path to POSIX path of (selected_item as alias)
set posix_path to quoted form of posix_path
end tell
set file_path to selected_item as string
set file_ext to ""
set file_name to ""
set text item delimiters to "."
if (count text items of file_path) > 1 then
set file_ext to last text item of file_path
set file_path to (text items 1 through -2 of file_path) as string
end if
set text item delimiters to ":"
if (count text items of file_path) > 1 then
set file_name to last text item of file_path
set file_path to ((text items 1 through -2 of file_path) as string) & ":"
end if
return {posix_path, file_path, file_name, file_ext}
end splitPath
运行应用:
保存后,您可以将其作为普通应用程序运行。
您也可以从脚本编辑器中运行它,但必须使用 alt + 运行按钮、 alt + R 或使用菜单脚本->运行应用程序。
关于applescript - 使用 AppleScript 和 Automator 创建图像注释工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29678737/
还有人在用applescript吗,苹果把文档存档了,好久没更新语言了。这是否意味着语言已死? 最佳答案 自从苹果在 2016 年解散 Mac 自动化团队并(迟迟)解雇了负责它的产品经理以来,整个 A
我无法弄清楚如何检测 AppleScript 中按下的键以及如何延迟直到该键被释放。我想切换缩放,我拥有其他一切(我认为)。这是我当前的代码 on idle set ztoggle to 0
是否可以为使用 AppleScript 创建的应用程序保存某种设置? 设置应在脚本开始时加载,并在脚本结束时保存。 例子: if loadSetting("timesRun") then se
我正在尝试显示我选择的文件的扩展名。我做错了什么? set theFile to (choose file with prompt "Select a file to transfer:")
我正在尝试制作一个将在后台运行并且每周只在特定时间执行一次的 applescript,有什么建议吗? 最佳答案 您可能想要的是 idle handler .首次打开 AppleScript 应用程序时
我希望能够引用与 Controller 位于同一目录中的模型文件。 最初,它们位于项目的根文件夹中,但当它们被编译(使用 osacompile)时,它们都将位于 Controller.scptd/Co
我不知道如何使用 AppleScript,一开始我是如何让我的小 bash 脚本工作的,这超出了我的能力。我目前正在使用 AppleScript 来运行我的 bash 脚本,它工作得非常好。它在下面。
我有一系列 AppleScript 命令需要在一行 AppleScript 中实现。代码如下。 delay 2 tell application "System Events" to keystrok
在 VBA 中,用不同的子字符串替换子字符串非常容易,例如 objMsg.Body = Replace(objMsg.Body, ""在电子邮件正文中使用当前月份。我以前见过与此类似的问题,但它们已经
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我已被任命创建一个必须 当网页加载完后,显示一个对话框,显示"The webpage has finished loading." 确定当前正在下载的项目数量 对于数字1,我尝试做if the U
我有一个用于创建作业文件夹的 Applescript。 Applescript 要求提供项目名称和文件夹的存储位置。输入作业名称和文件夹位置后,脚本会创建主文件夹和四个子文件夹。 现在我希望脚本在当前
我有一个字符串,其中包含要删除的非法字符,但我不知道可能存在哪种字符。 我建立了一个我不希望被过滤的字符列表,并建立了这个脚本(来自我在网络上找到的另一个脚本)。 on clean_string(Th
我很困惑 - 我已经用谷歌搜索了一个小时,并尝试了大约十种不同形式的 set posixDirectory to POSIX path of (parent of (path to aFile) as
我在 Automator 中有一个小的 applescript: do shell script "osascript ~/Focus-On.scpt" delay 60 do shell scrip
我想打开一个默认为应用程序设置文件夹的文件夹: /Users/XXX/Library/Application Support/Tunnelblick/Configurations 我不想“硬编码”“X
我试图找出最前面的应用程序 x 是什么,运行脚本,然后使用 applescript 将 x 设置为最前面的应用程序。 tell application "System Events" set
有一个 AppleScript 方法: on displayError(theErrorMessage) display dialog theErrorMessage return "
我正在尝试编写一个脚本来记录当前应用程序、切换到另一个应用程序、执行一些任务并返回到原始应用程序。这就是我所拥有的 set currentApp to my getCurrentApp() activ
我正在尝试根据其文本使用 AppleScript 选择大纲的特定行。 这是我正在考虑的(但不起作用): repeat with aRow in rows of outline 1 of scroll
我是一名优秀的程序员,十分优秀!