On Mac, is it possible to use AppleScript/Automator to show a preview of an image in the background, while the Automator app is running?
I have hundreds of images to which I want to add meta data so that they can be displayed on a web site. My plan is to create a *.meta file with the same name as the original image, so that a PHP script can read in the meta data at the same time that it generates the URL for the image.
I have created an AppleScript file (see below), which I have embedded in an Automator app. When you drop a selection of files on the app, it first shows you the image, and then shows 3 dialogs where you can enter the required data.
The problem is that the AppleScript is blocked while the qlmanage preview window is open. Before you can enter the required data, you need to close the window, so you can no longer see the image.
Is there a way to send the qlmanage process into the background so that the dialog windows can appear while the image is open?
Or perhaps there is already a free tool for Mac OS that already allows you to do exactly what I hope to do.
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
I wrote an Application that does most of what you do using ApplescriptOBJc in Script Editor.app ( mainly to see how easy it was ( It was ))
Gets a selection of images.
Populates text fields with info.
shows each image.
writes to a meta file.
ApplescriptOBJc is the syntax bridging language between Applescript and Objective - c.
This language allows you to access the power of Objective -c and use it alongside Applescript in the same code.
The only thing I have not added is a check for video files.
But as this is a example/get you started code you should be able to do that easily enough.
I also would normally use ApplescriptOBJc to write the files out but used your code for familiarity
The code should be easy enough to follow, change , add or move UI objects and also change or update its logic.
Past the code into a new Script Editor document. Then Save it as a stay open application.
The Code for the App:
-- 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
Running the App:
Once saved you run it as a normal application.
You can also run it from within Script Editor but must Run it using either alt + the run button, alt + R or use the menu Script->Run Application.
You should be able to send any shell script to the background by appending the ampersand to it. However, with AppleScript’s do shell script you also need to redirect the output somewhere. In your case, something like this should work:
do shell script "qlmanage -t -s 640 " & posix_path & "&>/dev/null &"
How that will affect this particular app will depend on that app.
Here is a simple example in AppleScript:
do shell script "say Hello. You can see the dialog while I speak. &>/dev/null &"
display dialog "Back from shell script."
The &> redirects the output to /dev/null (that is, to nowhere, basically) and the & sends the script to the background. I’m sending output to /dev/null because there is no output from the say command. In your case, if there is useful output, you might want to put the full path to somewhere you can see, such as /Users/YOURUSERNAME/Desktop/qlmanage.txt.
More about using shell scripts with AppleScript at https://developer.apple.com/library/mac/technotes/tn2065/_index.html
I have added this as a second answer so as not to pollute the original version of this code.
It bugged me a little that the original OP included mp4 type files and my original version did not address that.
I also wanted to have a way of overwriting the meta file if I wanted without having to delete it in the file system..
Lastly some feed back would be great..
The hardest part was figuring out how to get an image from a mp4 video file.
I could have gone down the route of using Objective - C framworks for 'AV' but to be honest I struggled in the time frame I gave myself to figure this all out and in the end remembered that the unix QuickLook command qlmange can actually do this for us.
So the video image snapshot is written to the users tmp folder and referenced in the image viewer from there. ta da..
Again this is a working example and as with any examples the end user may need to adjust to their needs.
Main changes
Added file type to accept mp4.
Added a Feed back text area
Added an overwrite file option
Updated the image counter to include the file
name.
-- 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
Related
This is useful when you have a folder of images that may contain images that you already have in your library.
on run
set folderList to (choose folder with multiple selections allowed)
tell application "Photos"
activate
delay 2
end tell
repeat with baseFolder in folderList
importFotos(baseFolder, "NEW ALBUM")
end repeat
end run
on importFotos(aFolder, albumName)
set imageList to getImageList(aFolder)
log (albumName)
if imageList is {} then return
set fotoAlbum to createFotoAlbum(albumName)
repeat with image in imageList
set fileInfo to info for image
set filename to name of fileInfo
set filenameStr to filename as string
log (filename)
tell application "Photos"
activate
set photosImage to search for filenameStr
add photosImage to fotoAlbum
log (photosImage)
end tell
end repeat
log (albumName)
end importFotos
on getImageList(aFolder)
set extensionsList to {"jpg", "png", "tiff", "JPG", "jpeg", "gif", "JPEG", "PNG", "TIFF", "GIF", "MOV", "mov", "MP4", "mp4", "M4V", "m4v", "MPG", "mpg", "BMP", "bmp", "TIF", "tif", "AVI", "avi", "PSD", "psd", "ai", "AI", "orf", "ORF", "nef", "NEF", "crw", "CRW", "cr2", "CR2", "dng", "DNG", "PEF", "HEIC"}
with timeout of (30 * 60) seconds
tell application "Finder" to set theFiles to every file of aFolder whose name extension is in extensionsList
end timeout
set imageList to {}
repeat with i from 1 to number of items in theFiles
set thisItem to item i of theFiles as alias
set the end of imageList to thisItem
end repeat
imageList
end getImageList
on createFotoAlbum(albumName)
tell application "Photos"
make new album named albumName
end tell
end createFotoAlbum
This is also available from my GitHub repository: https://github.com/vjsingh/make-album-without-importing
I have baby photos in a folder and I want to upload them one at a time about every hour (4000 secs not 3600 secs) to a shared iCloud album that all my relatives see on their iPhones and iPads and macs. Here is my applescript saved as an application with the keep open box checked. I think it's not quite right. What's wrong?
on idle
set importFolder to "Amac:Users:AbuDavid:Downloads:uploadBABY"
set extensionsList to {"jpg", "png", "tiff"}
tell application "Finder" to set theFiles to some file of importFolder whose name extension is in extensionsList
if (count of theFiles) < 1 then
display dialog "No images selected!" buttons "OK"
else
set albumName to "BabyDouDou"
set timeNow to time string of (current date)
set today to date string of (current date)
set albumName to albumName & " " & timeNow & " " & today
set imageList to theFiles
tell application "Photos"
activate
delay 2
import imageList into albumName skip check duplicates yes
end tell
tell application "Finder" to move theFiles to trash
end if
return 4000
end idle
There are some issues:
The Finder needs the keyword folder – not just a literal string – to specify a folder.
some file returns always one file so the count command fails and returns always 0.
albumName is a literal string as well rather than a object specifier.
Photos.app expects alias specifiers for the files to be imported rather than Finder object specifiers.
Try this
on idle
set importFolder to (path to downloads folder as text) & "uploadBABY"
set extensionsList to {"jpg", "png", "tiff"}
tell application "Finder" to set theFiles to files of folder importFolder whose name extension is in extensionsList
if (count theFiles) < 1 then
display dialog "No images selected!" buttons "OK"
else
set theFile to some item of theFiles
set albumName to "BabyDouDou"
set timeNow to time string of (current date)
set today to date string of (current date)
set albumName to albumName & " " & timeNow & " " & today
set imageList to {theFile as alias}
tell application "Photos"
activate
delay 2
if not (exists container albumName) then
set theAlbum to make new album
set name of theAlbum to albumName
else
set theAlbum to container albumName
end if
import imageList into theAlbum skip check duplicates yes
end tell
tell application "Finder" to move theFiles to trash
end if
return 4000
end idle
Made a small change to only delete the image that was uploaded and not all the images. Thank you so much.
on idle
set importFolder to (path to downloads folder as text) & "uploadBABY"
set extensionsList to {"jpg", "png", "tiff"}
tell application "Finder" to set theFiles to files of folder importFolder whose name extension is in extensionsList
if (count theFiles) < 1 then
display dialog "No images selected!" buttons "OK"
else
set theFile to some item of theFiles
set albumName to "testscript"
set imageList to {theFile as alias}
tell application "Photos"
activate
delay 2
if not (exists container albumName) then
set theAlbum to make new album
set name of theAlbum to albumName
else
set theAlbum to container albumName
end if
import imageList into theAlbum skip check duplicates yes
end tell
tell application "Finder" to move theFile to trash
end if
return 7
end idle
I am trying to achieve some automation in my current workflow. I'd like to drop images to my applescript application, then have it autoresize to the size as set by the user.
I prompt the user a dialog, where one can enter the pixelsize for width, then have the script do the donkey work.
For some reason it is not running properly, and for the life of my i can't figure out why.
Any and all help is very much appreciated!
Here's the script:
display dialog "What is the desired size?" default answer "64"
set my_answer to text returned of result
on open some_items
repeat with this_item in some_items
try
rescale_and_save(this_item)
end try
end repeat
end open
to rescale_and_save(this_item)
tell application "Image Events"
launch
set the target_width to my_answer
-- open the image file
set this_image to open this_item
set typ to this_image's file type
copy dimensions of this_image to {current_width, current_height}
if current_width is greater than current_height then
scale this_image to size target_width
else
-- figure out new height
-- y2 = (y1 * x2) / x1
set the new_height to (current_height * target_width) / current_width
scale this_image to size new_height
end if
tell application "Finder" to set new_item to ¬
(container of this_item as string) & "scaled." & (name of this_item)
save this_image in new_item as typ
end tell
end rescale_and_save
I wrote this script a while ago. It will re-sample an image so its largest dimension is the input value.
on open of myFiles
set my_answer to text returned of (display dialog "What is the desired size?" default answer "64")
repeat with aFile in myFiles
set filePath to aFile's POSIX path
set bPath to (do shell script "dirname " & quoted form of filePath)
tell application "System Events" to set {fileName, fileExt} to {name, name extension} of aFile
set baseName to text 1 thru ((get offset of "." & fileExt in fileName) - 1) of fileName
do shell script "sips " & quoted form of aFile's POSIX path & " -Z " & my_answer & " --out " & quoted form of (bPath & "/" & baseName & "-" & my_answer & "." & fileExt as text)
end repeat
end open
I am working on finishing up an script droplet to pull all used swatches out of multiple indesign documents that I drop on the script and list them. I am adapting this from a script I wrote to put all used swatches from a single file.
I have the basics down but have run into two problems and cant find an answer.
Problem 1: I need to add the line to "repeat until swatchCount < 2" but this would ad a repeat inside a repeat. Doesn't work.
Problem 2: (the biggest problem) The "colorList" needs to add the colors from each of the documents together into a single list. Since I have it on repeat it is only listing the colors from the last processed file. How do I have the list save each files color and then list them all?
I would appreciate any help I have hit a wall.
on open these_items
repeat with i from 1 to the count of these_items
set this_item to item i of these_items
set the item_info to info for this_item
set this_item to POSIX path of this_item
tell application "Adobe InDesign CC"
open this_item
tell active document
delete unused swatches
set allSwatches to every swatch
end tell
set swatchCount to count of allSwatches
set colorList to ""
-- repeat until swatchCount < 2
set thisSwatch to item swatchCount of allSwatches
try
set swatchProps to type of thisSwatch
set swatchProps to "Gradient"
on error
set swatchProps to "Color"
end try
if swatchProps = "Color" then
set swatchName to name of thisSwatch as string
set swatchType to model of thisSwatch as string
set swatchSpace to space of thisSwatch as string
set swatchColor to color value of thisSwatch
if swatchType contains "spot" then
set swatchType to "Spot"
else
if swatchType contains "prss" then
set swatchType to "Process"
end if
end if
if swatchSpace contains "RGB" then
set rrr to ((item 1 of swatchColor) as integer)
set ggg to ((item 2 of swatchColor) as integer)
set bbb to ((item 3 of swatchColor) as integer)
set swatchMix to (" value: R-" & rrr & " G-" & ggg & " B-" & bbb) as string
if swatchName does not contain "registration" then
set swatchItem to "Name: " & swatchName & swatchMix & " space: RGB" & " type: " & swatchType & return
else
set swatchItem to ""
end if
else
if swatchSpace contains "CMYK" then
set ccc to ((item 1 of swatchColor) as integer)
set mmm to ((item 2 of swatchColor) as integer)
set yyy to ((item 3 of swatchColor) as integer)
set kkk to ((item 4 of swatchColor) as integer)
set swatchMix to (" value: C-" & ccc & " M-" & mmm & " Y-" & yyy & " K-" & kkk) as string
if swatchName does not contain "registration" then
set swatchItem to "Name: " & swatchName & swatchMix & " space:CMYK" & " type: " & swatchType & return
else
set swatchItem to ""
end if
else
if swatchSpace contains "LAB" then
set lll to ((item 1 of swatchColor) as integer)
set aaa to ((item 2 of swatchColor) as integer)
set bbb to ((item 3 of swatchColor) as integer)
set swatchMix to (" value: L-" & lll & " A-" & aaa & " B-" & bbb) as string
if swatchName does not contain "registration" then
set swatchItem to "Name: " & swatchName & swatchMix & " space: LAB" & " type: " & swatchType & return
else
set swatchItem to ""
end if
end if
end if
end if
set colorList to colorList & swatchItem
end if
tell active document to close saving no
set swatchCount to swatchCount - 1
--end repeat
end tell
end repeat
set dragged_items to these_items as string
set old_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set theClientName to text item 2 of dragged_items
set theFileName to text item 5 of dragged_items
set AppleScript's text item delimiters to old_delimiters
set theOrderNumber to text 1 through 5 of theFileName
set headerInfo to "Client: " & theClientName & " Order # " & theOrderNumber & " File: " & theFileName
tell application "Adobe InDesign CC"
tell view preferences
set horizontal measurement units to inches
set vertical measurement units to inches
end tell
make new document with properties {document preferences:{page width:8.5, page height:11}}
tell view preferences
set horizontal measurement units to inches
set vertical measurement units to inches
end tell
tell active document
set zero point to {0, 0}
set properties of guide preferences to {guides shown:false}
set properties of text preferences to {show invisibles:false}
set properties of view preferences to {ruler origin:page origin, horizontal measurement units:inches, show frame edges:false, show rulers:true, vertical measurement units:inches}
tell layout window 1
zoom given fit page
set properties to {view display setting:typical, transform reference point:top left anchor}
end tell
make new text frame with properties {geometric bounds:{0.5, 0.5, 0.75, 8.0}, contents:headerInfo, color:"None"}
set paragraphCount to count of paragraphs of colorList
repeat until paragraphCount > 19
set colorList to colorList & "name: _________________________" & " value: ____________________" & " space: ______" & " type: _________" & return
set paragraphCount to count of paragraphs of colorList
end repeat
make new text frame with properties {geometric bounds:{4.5, 0.5, 20.0, 8.0}, contents:colorList, color:"None"}
tell text frame 1
tell every text to set point size to 10
tell every paragraph
make tab stop with properties {alignment:left align, position:2.875}
make tab stop with properties {alignment:left align, position:5.125}
make tab stop with properties {alignment:left align, position:6.375}
end tell
set geometric bounds to {1.0, 0.5, 11, 8.0}
end tell
end tell
end tell
end open
on run
-- Handle the case where the script is launched without any dropped files
open (choose file with multiple selections allowed)
end run
I have added the changes that you suggested "Darrick Herwehe" but am getting an error now on a different part.
Can’t get name of color id 344 of document id 30
--name of color id 344 of document id 29 of application "Adobe InDesign CC"--
getting this in reference to: set swatchName to name of thisSwatch
on open these_items
set colorList to ""
set swatchItem to ""
repeat with i from 1 to the count of these_items
--Get swatches from document start
set this_item to item i of these_items
set the item_info to info for this_item
set this_item to POSIX path of this_item
tell application "Adobe InDesign CC"
open this_item
tell active document
delete unused swatches
set allSwatches to every swatch
end tell
set swatchCount to count of allSwatches
repeat until swatchCount < 2
repeat with j from 1 to (count allSwatches)
set thisSwatch to item swatchCount of allSwatches
try
set swatchProps to type of thisSwatch
set swatchProps to "Gradient"
on error
set swatchProps to "Color"
end try
if swatchProps = "Color" then
set swatchName to name of thisSwatch
set swatchType to model of thisSwatch
set swatchSpace to space of thisSwatch
set swatchColor to color value of thisSwatch
if swatchType contains "spot" then
set swatchType to "Spot"
else
if swatchType contains "prss" then
set swatchType to "Process"
end if
end if
if swatchSpace contains "RGB" then
set rrr to ((item 1 of swatchColor) as integer)
set ggg to ((item 2 of swatchColor) as integer)
set bbb to ((item 3 of swatchColor) as integer)
set swatchMix to (" value: R-" & rrr & " G-" & ggg & " B-" & bbb) as string
if swatchName does not contain "registration" then
set swatchItem to "Name: " & swatchName & swatchMix & " space: RGB" & " type: " & swatchType & return
else
set swatchItem to ""
end if
else
if swatchSpace contains "CMYK" then
set ccc to ((item 1 of swatchColor) as integer)
set mmm to ((item 2 of swatchColor) as integer)
set yyy to ((item 3 of swatchColor) as integer)
set kkk to ((item 4 of swatchColor) as integer)
set swatchMix to (" value: C-" & ccc & " M-" & mmm & " Y-" & yyy & " K-" & kkk) as string
if swatchName does not contain "registration" then
set swatchItem to "Name: " & swatchName & swatchMix & " space:CMYK" & " type: " & swatchType & return
else
set swatchItem to ""
end if
else
if swatchSpace contains "LAB" then
set lll to ((item 1 of swatchColor) as integer)
set aaa to ((item 2 of swatchColor) as integer)
set bbb to ((item 3 of swatchColor) as integer)
set swatchMix to (" value: L-" & lll & " A-" & aaa & " B-" & bbb) as string
if swatchName does not contain "registration" then
set swatchItem to "Name: " & swatchName & swatchMix & " space: LAB" & " type: " & swatchType & return
else
set swatchItem to ""
end if
end if
end if
end if
tell active document to close saving no
set colorList to colorList & swatchItem
end if
set swatchCount to swatchCount - 1
end repeat
end repeat
end tell
end repeat
set dragged_items to these_items as string
set old_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set theClientName to text item 2 of dragged_items
set theFileName to text item 5 of dragged_items
set AppleScript's text item delimiters to old_delimiters
set theOrderNumber to text 1 through 5 of theFileName
set headerInfo to "Client: " & theClientName & " Order # " & theOrderNumber & " File: " & theFileName
tell application "Adobe InDesign CC"
tell view preferences
set horizontal measurement units to inches
set vertical measurement units to inches
end tell
make new document with properties {document preferences:{page width:8.5, page height:11}}
tell view preferences
set horizontal measurement units to inches
set vertical measurement units to inches
end tell
tell active document
set zero point to {0, 0}
set properties of guide preferences to {guides shown:false}
set properties of text preferences to {show invisibles:false}
set properties of view preferences to {ruler origin:page origin, horizontal measurement units:inches, show frame edges:false, show rulers:true, vertical measurement units:inches}
tell layout window 1
zoom given fit page
set properties to {view display setting:typical, transform reference point:top left anchor}
end tell
make new text frame with properties {geometric bounds:{0.5, 0.5, 0.75, 8.0}, contents:headerInfo, color:"None"}
set paragraphCount to count of paragraphs of colorList
repeat until paragraphCount > 19
set colorList to colorList & "name: _________________________" & " value: ____________________" & " space: ______" & " type: _________" & return
set paragraphCount to count of paragraphs of colorList
end repeat
make new text frame with properties {geometric bounds:{4.5, 0.5, 20.0, 8.0}, contents:colorList, color:"None"}
tell text frame 1
tell every text to set point size to 10
tell every paragraph
make tab stop with properties {alignment:left align, position:2.875}
make tab stop with properties {alignment:left align, position:5.125}
make tab stop with properties {alignment:left align, position:6.375}
end tell
set geometric bounds to {1.0, 0.5, 11, 8.0}
end tell
end tell
end tell
end open
on run
-- Handle the case where the script is launched without any dropped files
open (choose file with multiple selections allowed)
end run
If you have any insight I would much appriciate it.
Thanks again
Matt
You can nest repeat loops. It's not a problem, it's done all the time. You just have to define a different iteration variable from your first repeat loop. In your case, I would use j.
As for getting all of the colors into one list, you neeed to define colorList outside your first repeat loop. That way it doesn't get overwritten during each iteration.
An outline for that would be:
on open these_items
set colorList to ""
repeat with i from 1 to the count of these_items
-- [ Get swatches from the document ]
repeat with j from 1 to (count allSwatches)
-- [ Process your swatches ]
set colorList to colorList & swatchItem
end repeat
end repeat
end open
Anyone of you guys could point me to why this applescript (which was working on snow leopard) does not work anymore on 10.7 Lion.
It intend to copy the file named "folder.jpg" from each album folder into ID3 tags of selected mp3 in iTunes.
property tempfile : ((path to temporary items as string) & "itunespicturefile_temporaire.pict")
global vPictFolder
tell application "iTunes"
set v_TrackSelection to selection
if v_TrackSelection is not {} then
repeat with vTrack in v_TrackSelection
set vPictFolder to my (ParentFromPath(location of vTrack as string)) as text
set thisPict to my getpictData("folder.jpg")
if thisPict is not "" then
delete artworks of vTrack
set data of artwork 1 of vTrack to (thisPict)
end if
end repeat
else
display dialog ("select tracks first !")
end if
end tell
on getpictData(vAlbumpictName)
tell application "Finder" to tell file vAlbumpictName of folder vPictFolder to if exists then
set t_file to it as string
else
display dialog vPictFolder & vAlbumpictName
return ""
end if
do shell script "/opt/local/bin/convert " & quoted form of POSIX path of t_file & " " & quoted form of POSIX path of tempfile
display dialog "ok"
return read (tempfile as alias) from 513 as picture
end getpictData
on ParentFromPath(thePath)
set wantPath to true
set thePath to (thePath as text)
set saveDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set pathAsList to text items of thePath
if the last character of thePath is ":" then
set idx to (the number of text items in thePath) - 2
else
set idx to -2
end if
if wantPath then
set folderName to ((text items 1 through idx of pathAsList) as text) & ":"
else
set folderName to item idx of pathAsList
end if
set AppleScript's text item delimiters to saveDelim
return folderName
end ParentFromPath
Because Picture format (".pict") is not supported on Lion and newer OS.
You can use the term picture to read a image file (JPEG, PNG, ...), the format will not be in the Picture format , but the original format of the file.
tell application "iTunes"
set v_TrackSelection to selection
if v_TrackSelection is not {} then
repeat with vTrack in v_TrackSelection
set vPictFolder to my (ParentFromPath(location of vTrack as string)) as text
set thisPict to my getpictData(vPictFolder & "folder.jpg")
if thisPict is not "" then
delete artworks of vTrack
set data of artwork 1 of vTrack to (thisPict)
end if
end repeat
else
display dialog ("select tracks first !")
end if
end tell
on getpictData(tFile)
try
return (read (tFile as alias) as picture)
end try
display dialog tFile
return "" -- not exists
end getpictData