使用 AppleScript 和 Automator 创建图像注释工具
Create an image annotation tool with AppleScript and Automator
在 Mac 上,是否可以使用 AppleScript/Automator 在后台显示图像预览,而 Automator 应用是 运行?
我有数百张图片,我想向其中添加元数据,以便它们可以显示在网站上。我的计划是创建一个与原始图像同名的 *.meta
文件,以便 PHP 脚本可以在为 URL 生成 URL 的同时读取元数据图片。
我创建了一个 AppleScript 文件(见下文),并将其嵌入到 Automator 应用程序中。当您将选择的文件拖放到应用程序上时,它首先会显示图像,然后显示 3 个对话框,您可以在其中输入所需的数据。
问题是 AppleScript 在 qlmanage 预览 window 打开时被阻止。在输入所需数据之前,您需要关闭 window,因此您无法再看到图像。
有没有办法将 qlmanage 进程发送到后台,以便在图像打开时出现对话框 windows?
或者也许 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
您应该能够将任何 shell 脚本通过附加符号发送到后台。但是,对于 AppleScript 的 do shell script
,您还需要将输出重定向到某处。在你的情况下,这样的事情应该有效:
do shell script "qlmanage -t -s 640 " & posix_path & "&>/dev/null &"
这将如何影响这个特定的应用程序将取决于那个应用程序。
这是 AppleScript 中的一个简单示例:
do shell script "say Hello. You can see the dialog while I speak. &>/dev/null &"
display dialog "Back from shell script."
&>
将输出重定向到 /dev/null (也就是说,基本上无处可去), &
将脚本发送到后台。我将输出发送到 /dev/null,因为 say
命令没有输出。在您的情况下,如果有有用的输出,您可能希望将完整路径放在您可以看到的地方,例如 /Users/YOURUSERNAME/Desktop/qlmanage.txt
.
有关将 shell 脚本与 AppleScript 结合使用的更多信息,请访问 https://developer.apple.com/library/mac/technotes/tn2065/_index.html
我写了一个应用程序,它完成了你在脚本中使用 ApplescriptOBJc 所做的大部分工作 Editor.app(主要是为了看看它有多容易(它是))
- 获取图像的选择。
- 用信息填充文本字段。
- 显示每张图片。
- 写入元文件。
ApplescriptOBJc 是 Applescript 和 Objective - c.
之间的语法桥接语言
这种语言允许您获得 Objective -c 的强大功能,并在同一代码中将其与 Applescript 一起使用。
我唯一没有添加的是检查视频文件。
但由于这是 example/get 您开始的代码,您应该能够很容易地做到这一点。
我通常也会使用 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 或者使用菜单Script->运行 Application.
我将此添加为第二个答案,以免污染此代码的原始版本。
最初的 OP 包含 mp4 类型的文件,而我的原始版本没有解决这个问题,这让我有点烦恼。
我还想有一种方法可以覆盖 meta 文件,如果我愿意的话,而不必在文件系统中删除它..
最后一些反馈会很棒..
最难的部分是弄清楚如何从 mp4 视频文件中获取图像。
我本可以使用 Objective - 'AV' 的 C 框架,但老实说,我在给自己的时间范围内挣扎,最终弄清楚了这一切记得 unix QuickLook 命令 qlmange 实际上可以为我们做到这一点。
所以视频图像快照被写入用户的 tmp 文件夹,并在图像查看器中从那里引用。哒哒..
这也是一个工作示例,与任何示例一样,最终用户可能需要根据自己的需要进行调整。
主要变化
- 添加了接受 mp4 的文件类型。
- 添加了反馈文本区域
- 添加了覆盖文件选项
- 更新了图像计数器以包含文件
名字.
-- 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 feedBacklField : 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"
property checkBoxOverWriteBtn : class "NSButton"
property NSFileManager : class "NSFileManager"
property file_types : {"public.mpeg-4", "public.image"}
set NSFileManager to current application's NSFileManager's defaultManager
set height to 700
set width to 1000
set textFieldGap to 10
set labelFiieldGap to 5
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(155, (height - 270), 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 checkBoxOverWriteButtonFrame to current application's NSMakeRect(25, (height - 240), 185, 50) -- button rect origin ,x,y ,size width,hieght
set checkBoxOverWriteBtn to current application's NSButton's alloc's initWithFrame:checkBoxOverWriteButtonFrame -- init button
checkBoxOverWriteBtn's setTitle:"Overight meta file if it Exists"
checkBoxOverWriteBtn's setButtonType:3 --NSSwitchButtonn
checkBoxOverWriteBtn's setTarget:me
--checkBoxOverWriteBtn's setAction:"checkBoxOverWriteFileSetState:"
checkBoxOverWriteBtn's setState:0
checkBoxOverWriteBtn's setAlignment:0
checkBoxOverWriteBtn's setImagePosition:3
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
buttonWindow's contentView's addSubview:checkBoxOverWriteBtn
--
-- 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 - 105)), 240, 50) --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 - 130), 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 - 150), 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 - 180), 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 - 200), 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(320, (height - 25), 560, 25) --origin ,x,y ,size width,hieght
set countLabelField to current application's NSTextField's alloc()'s initWithFrame:countLabelFrame
countLabelField's setStringValue:"-"
countLabelField's setAlignment:2 --center
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)
set feedBacklFrame to current application's NSMakeRect(25, (height - 450), 240, 120) --origin ,x,y ,size width,hieght
set feedBacklField to current application's NSTextField's alloc()'s initWithFrame:feedBacklFrame
feedBacklField's setStringValue:""
feedBacklField's setEditable:false
buttonWindow's contentView's addSubview:feedBacklField
---
on nextImage:sender
feedBacklField's setStringValue:""
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
feedBacklField's setStringValue:""
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 theSender to (current application's NSString's stringWithString:("setuptFields" as string))
set {thisFile_name, file_ext} to splitPath((item thisImage of imageListSplit), theSender)
set altField's stringValue to thisFile_name
set copyrightField's stringValue to copyright
set sigField's stringValue to signature
--display dialog ( file_ext as string)
set countLabelField's stringValue to ((thisFile_name as string) & "." & file_ext as string) & " -|- " & (thisImage & " of " & (count of imageListSplit) as string)
end setUpTextFieldsStrings
on writeFile:sender
set {posix_path, file_path, file_name, file_ext} to splitPath((item thisImage of imageListSplit), sender)
set meta_path to file_path & file_name & ".meta"
---display dialog meta_path
set overWriteState to ""
set overWriteState to checkBoxOverWriteBtn's state
set alreadyExists to false
set alreadyExists to NSFileManager's fileExistsAtPath:meta_path
if alreadyExists then
if not overWriteState then
set success to (file_name as string) & ".meta " & return & return & "Already exists in this location" & return & "It was not Overwritten"
feedBacklField's setStringValue:(success as string)
return
end if
end if
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 writeString to current application's NSString's stringWithString:meta_text
set stringEncoding to 4
--set success to writeString's writeToFile:(POSIX path of meta_path) atomically:true encoding:stringEncoding |error|:(missing value)
set theData to writeString's dataUsingEncoding:stringEncoding
--SETTING THE TYPECODE AS (unsigned long = 1413830740 )plain text allows QuickLook to read the file and TextEdit.app open it as default
set theAttributes to current application's NSDictionary's dictionaryWithDictionary:{NSFileHFSTypeCode:1.41383074E+9}
set success to NSFileManager's createFileAtPath:(POSIX path of meta_path) |contents|:theData attributes:theAttributes
if success then
if alreadyExists then
set writenOut to "Already exists and was Overwritten"
else
set writenOut to "Was written to file"
end if
set success to (file_name as string) & ".meta " & return & return & writenOut
else
set success to "Error: " & (file_name as string) & ".meta " & return & return & "Was not written to file"
end if
feedBacklField's setStringValue:(success as string)
--end tell
end writeFile:
on finderSelection:sender
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))
--Check_types
repeat with ii from 1 to number of items in file_types
set this_type to item ii of file_types
if (workSpace's type:type conformsToType:this_type) then
set end of imageListSplit to item i of the_selection
if this_type is "public.mpeg-4" then
tell application "System Events" to set save_path to POSIX path of (temporary items folder)
do shell script ("/usr/bin/qlmanage -t -s640 " & quoted form of this_item & space & " -o " & quoted form of save_path)
set theSender to (current application's NSString's stringWithString:("videoFile" as string))
set {movieFileName} to splitPath((item i of the_selection), theSender)
set end of imageList to (current application's NSImage's alloc()'s initWithContentsOfFile:(save_path & "/" & movieFileName & ".png" as string))
else
set end of imageList to (current application's NSImage's alloc()'s initWithContentsOfFile:this_item)
end if
exit repeat
end if
end repeat
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, sender)
set file_ext to ""
set file_name to ""
set posix_path to ""
set posix_path to POSIX path of (selected_item as alias)
set posix_path to current application's NSString's stringWithString:(posix_path as string)
set file_path to ((posix_path's stringByDeletingLastPathComponent) as string) & "/"
set file_name to posix_path's lastPathComponent
set file_ext to file_name's pathExtension
set file_name to file_name's stringByDeletingPathExtension
if (sender's className as string) is "NSButton" then
return {posix_path, file_path, file_name, file_ext}
else if ((sender as string) is "setuptFields") then
return {file_name, file_ext}
else if ((sender as string) is "videoFile") then
return {posix_path's lastPathComponent}
end if
end splitPath
在 Mac 上,是否可以使用 AppleScript/Automator 在后台显示图像预览,而 Automator 应用是 运行?
我有数百张图片,我想向其中添加元数据,以便它们可以显示在网站上。我的计划是创建一个与原始图像同名的 *.meta
文件,以便 PHP 脚本可以在为 URL 生成 URL 的同时读取元数据图片。
我创建了一个 AppleScript 文件(见下文),并将其嵌入到 Automator 应用程序中。当您将选择的文件拖放到应用程序上时,它首先会显示图像,然后显示 3 个对话框,您可以在其中输入所需的数据。
问题是 AppleScript 在 qlmanage 预览 window 打开时被阻止。在输入所需数据之前,您需要关闭 window,因此您无法再看到图像。
有没有办法将 qlmanage 进程发送到后台,以便在图像打开时出现对话框 windows?
或者也许 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
您应该能够将任何 shell 脚本通过附加符号发送到后台。但是,对于 AppleScript 的 do shell script
,您还需要将输出重定向到某处。在你的情况下,这样的事情应该有效:
do shell script "qlmanage -t -s 640 " & posix_path & "&>/dev/null &"
这将如何影响这个特定的应用程序将取决于那个应用程序。
这是 AppleScript 中的一个简单示例:
do shell script "say Hello. You can see the dialog while I speak. &>/dev/null &"
display dialog "Back from shell script."
&>
将输出重定向到 /dev/null (也就是说,基本上无处可去), &
将脚本发送到后台。我将输出发送到 /dev/null,因为 say
命令没有输出。在您的情况下,如果有有用的输出,您可能希望将完整路径放在您可以看到的地方,例如 /Users/YOURUSERNAME/Desktop/qlmanage.txt
.
有关将 shell 脚本与 AppleScript 结合使用的更多信息,请访问 https://developer.apple.com/library/mac/technotes/tn2065/_index.html
我写了一个应用程序,它完成了你在脚本中使用 ApplescriptOBJc 所做的大部分工作 Editor.app(主要是为了看看它有多容易(它是))
- 获取图像的选择。
- 用信息填充文本字段。
- 显示每张图片。
- 写入元文件。
ApplescriptOBJc 是 Applescript 和 Objective - c.
之间的语法桥接语言这种语言允许您获得 Objective -c 的强大功能,并在同一代码中将其与 Applescript 一起使用。
我唯一没有添加的是检查视频文件。
但由于这是 example/get 您开始的代码,您应该能够很容易地做到这一点。
我通常也会使用 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 或者使用菜单Script->运行 Application.
我将此添加为第二个答案,以免污染此代码的原始版本。
最初的 OP 包含 mp4 类型的文件,而我的原始版本没有解决这个问题,这让我有点烦恼。
我还想有一种方法可以覆盖 meta 文件,如果我愿意的话,而不必在文件系统中删除它..
最后一些反馈会很棒..
最难的部分是弄清楚如何从 mp4 视频文件中获取图像。
我本可以使用 Objective - 'AV' 的 C 框架,但老实说,我在给自己的时间范围内挣扎,最终弄清楚了这一切记得 unix QuickLook 命令 qlmange 实际上可以为我们做到这一点。
所以视频图像快照被写入用户的 tmp 文件夹,并在图像查看器中从那里引用。哒哒..
这也是一个工作示例,与任何示例一样,最终用户可能需要根据自己的需要进行调整。
主要变化
- 添加了接受 mp4 的文件类型。
- 添加了反馈文本区域
- 添加了覆盖文件选项
- 更新了图像计数器以包含文件 名字.
-- 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 feedBacklField : 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"
property checkBoxOverWriteBtn : class "NSButton"
property NSFileManager : class "NSFileManager"
property file_types : {"public.mpeg-4", "public.image"}
set NSFileManager to current application's NSFileManager's defaultManager
set height to 700
set width to 1000
set textFieldGap to 10
set labelFiieldGap to 5
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(155, (height - 270), 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 checkBoxOverWriteButtonFrame to current application's NSMakeRect(25, (height - 240), 185, 50) -- button rect origin ,x,y ,size width,hieght
set checkBoxOverWriteBtn to current application's NSButton's alloc's initWithFrame:checkBoxOverWriteButtonFrame -- init button
checkBoxOverWriteBtn's setTitle:"Overight meta file if it Exists"
checkBoxOverWriteBtn's setButtonType:3 --NSSwitchButtonn
checkBoxOverWriteBtn's setTarget:me
--checkBoxOverWriteBtn's setAction:"checkBoxOverWriteFileSetState:"
checkBoxOverWriteBtn's setState:0
checkBoxOverWriteBtn's setAlignment:0
checkBoxOverWriteBtn's setImagePosition:3
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
buttonWindow's contentView's addSubview:checkBoxOverWriteBtn
--
-- 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 - 105)), 240, 50) --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 - 130), 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 - 150), 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 - 180), 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 - 200), 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(320, (height - 25), 560, 25) --origin ,x,y ,size width,hieght
set countLabelField to current application's NSTextField's alloc()'s initWithFrame:countLabelFrame
countLabelField's setStringValue:"-"
countLabelField's setAlignment:2 --center
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)
set feedBacklFrame to current application's NSMakeRect(25, (height - 450), 240, 120) --origin ,x,y ,size width,hieght
set feedBacklField to current application's NSTextField's alloc()'s initWithFrame:feedBacklFrame
feedBacklField's setStringValue:""
feedBacklField's setEditable:false
buttonWindow's contentView's addSubview:feedBacklField
---
on nextImage:sender
feedBacklField's setStringValue:""
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
feedBacklField's setStringValue:""
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 theSender to (current application's NSString's stringWithString:("setuptFields" as string))
set {thisFile_name, file_ext} to splitPath((item thisImage of imageListSplit), theSender)
set altField's stringValue to thisFile_name
set copyrightField's stringValue to copyright
set sigField's stringValue to signature
--display dialog ( file_ext as string)
set countLabelField's stringValue to ((thisFile_name as string) & "." & file_ext as string) & " -|- " & (thisImage & " of " & (count of imageListSplit) as string)
end setUpTextFieldsStrings
on writeFile:sender
set {posix_path, file_path, file_name, file_ext} to splitPath((item thisImage of imageListSplit), sender)
set meta_path to file_path & file_name & ".meta"
---display dialog meta_path
set overWriteState to ""
set overWriteState to checkBoxOverWriteBtn's state
set alreadyExists to false
set alreadyExists to NSFileManager's fileExistsAtPath:meta_path
if alreadyExists then
if not overWriteState then
set success to (file_name as string) & ".meta " & return & return & "Already exists in this location" & return & "It was not Overwritten"
feedBacklField's setStringValue:(success as string)
return
end if
end if
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 writeString to current application's NSString's stringWithString:meta_text
set stringEncoding to 4
--set success to writeString's writeToFile:(POSIX path of meta_path) atomically:true encoding:stringEncoding |error|:(missing value)
set theData to writeString's dataUsingEncoding:stringEncoding
--SETTING THE TYPECODE AS (unsigned long = 1413830740 )plain text allows QuickLook to read the file and TextEdit.app open it as default
set theAttributes to current application's NSDictionary's dictionaryWithDictionary:{NSFileHFSTypeCode:1.41383074E+9}
set success to NSFileManager's createFileAtPath:(POSIX path of meta_path) |contents|:theData attributes:theAttributes
if success then
if alreadyExists then
set writenOut to "Already exists and was Overwritten"
else
set writenOut to "Was written to file"
end if
set success to (file_name as string) & ".meta " & return & return & writenOut
else
set success to "Error: " & (file_name as string) & ".meta " & return & return & "Was not written to file"
end if
feedBacklField's setStringValue:(success as string)
--end tell
end writeFile:
on finderSelection:sender
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))
--Check_types
repeat with ii from 1 to number of items in file_types
set this_type to item ii of file_types
if (workSpace's type:type conformsToType:this_type) then
set end of imageListSplit to item i of the_selection
if this_type is "public.mpeg-4" then
tell application "System Events" to set save_path to POSIX path of (temporary items folder)
do shell script ("/usr/bin/qlmanage -t -s640 " & quoted form of this_item & space & " -o " & quoted form of save_path)
set theSender to (current application's NSString's stringWithString:("videoFile" as string))
set {movieFileName} to splitPath((item i of the_selection), theSender)
set end of imageList to (current application's NSImage's alloc()'s initWithContentsOfFile:(save_path & "/" & movieFileName & ".png" as string))
else
set end of imageList to (current application's NSImage's alloc()'s initWithContentsOfFile:this_item)
end if
exit repeat
end if
end repeat
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, sender)
set file_ext to ""
set file_name to ""
set posix_path to ""
set posix_path to POSIX path of (selected_item as alias)
set posix_path to current application's NSString's stringWithString:(posix_path as string)
set file_path to ((posix_path's stringByDeletingLastPathComponent) as string) & "/"
set file_name to posix_path's lastPathComponent
set file_ext to file_name's pathExtension
set file_name to file_name's stringByDeletingPathExtension
if (sender's className as string) is "NSButton" then
return {posix_path, file_path, file_name, file_ext}
else if ((sender as string) is "setuptFields") then
return {file_name, file_ext}
else if ((sender as string) is "videoFile") then
return {posix_path's lastPathComponent}
end if
end splitPath