How to show composite icons using Display Dialog - applescript

Is there any way to display Display Dialog icons like macOS does, I tried looking for the icon below in /System/Library/CoreServices/CoreTypes.bundle
but I didn't find it.
set myIcon to (path to resource "myIcon.icns")
display dialog "this is my icon" buttons {"OK"} default button "OK" with icon myIcon

All of the standard Apple icons are stored here:
`/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources`
You can access them directly, as in the following.
set iconFIle to choose file "Choose an icon file" of type {"com.apple.icns"} ¬
default location POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources"
display dialog "This is a test" with icon iconFIle
Note that the Finder sometimes does composite icons by using one icon as an overlay or badge applied to another icon. For instance, I believe the image you posted above is the file "KEXT.icns" overlaid with the file "AlertCautionBadgeIcon.icns." You can mimic that behavior with the following script.
set basePath to POSIX path of (path to library folder from system domain) & "CoreServices/CoreTypes.bundle/Contents/Resources/"
set tempStoragePath to POSIX path of (path to temporary items from user domain)
-- set the name of the main idon, and the overlay icon
set mainName to "KEXT"
set overlayName to "AlertCautionBadgeIcon"
set kextImgPath to basePath & mainName & ".icns"
set alertImgPath to basePath & overlayName & ".icns"
set tempSetPath to quoted form of (tempStoragePath & mainName & ".iconset" as text)
set tempOverlayPath to quoted form of (tempStoragePath & overlayName & ".iconset" as text)
-- decompose the icns into iconsets, so that we can do the overlays by hand
do shell script "iconutil -c iconset -o " & tempSetPath & " " & kextImgPath
do shell script "iconutil -c iconset -o " & tempOverlayPath & " " & alertImgPath
tell application "System Events"
set mainFolder to folder (mainName & ".iconset") of folder tempStoragePath
set overlayFolder to folder (overlayName & ".iconset") of folder tempStoragePath
set theFiles to (files of mainFolder whose name extension is "png")
(*
iconsets are folders with files named (e.g.) 'icon_16x16.png', 'icon_128x128#2x.png'
this loop runs through and finds matching sized elements of the set, then sends
them to the script object's handler for processing with ASOC
*)
repeat with thisFile in theFiles
set fileName to name of thisFile
if exists file (name of thisFile) of overlayFolder then
asocBits's mungePNGs(POSIX path of thisFile, POSIX path of (file fileName of overlayFolder))
end if
end repeat
end tell
-- convert the iconset folders back to icns files, then send the file to display dialog
do shell script "iconutil -c icns " & tempSetPath
display dialog "This is a test" with icon POSIX file (tempStoragePath & mainName & ".icns")
script asocBits
use framework "Foundation"
use framework "AppKit"
property NSImage : class "NSImage"
property NSBitmapImageRep : class "NSBitmapImageRep"
on mungePNGs(mainImg, overlay)
-- get respective images
set mainImgObj to NSImage's alloc's initWithContentsOfFile:mainImg
set overlayObj to NSImage's alloc's initWithContentsOfFile:overlay
-- create a destination image
set newImage to NSImage's alloc's initWithSize:(mainImgObj's |size|)
newImage's lockFocus()
-- set up apprpriate drawing rects, then draw the two images into the new image.
set newRect to current application's CGRectZero as list
set imgSize to mainImgObj's |size| as list
set item 2 of newRect to item 1 of imgSize
set h to (height of item 1 of imgSize)
(*
these were trial and error. remember that (0,0) is the bottom left,
not the top left (y goes from bottom to top).
placementRect is supposed to be where the overlay is placed.
cropRect is the part of the overlay that's pasted.
different badges have their effective images in different quadrants.
*)
set placementRect to current application's CGRectMake(0.5 * h, 0.0, h, h)
set cropRect to current application's CGRectMake(0.5 * h, 0.5 * h, h, h)
mainImgObj's drawInRect:newRect
set op to current application's NSCompositeSourceOver
overlayObj's drawInRect:placementRect fromRect:cropRect operation:op fraction:1.0
-- create a bitmap representation and save it back to the main iconset file
set bitmapRep to NSBitmapImageRep's alloc's initWithFocusedViewRect:newRect
newImage's unlockFocus()
set PNGType to current application's NSBitmapImageFileTypePNG
set imgData to (bitmapRep's representationUsingType:PNGType |properties|:(missing value))
imgData's writeToFile:mainImg atomically:false
end mungePNGs
end script
The script is on the slow side, but it gets the job done. In the long run, it might be more efficient simply to use the script object to create icns files that you can store in the bundle and call at need.
Technical detail: I've used a script object to isolate the ASOC routines from the rest of the script. ASOC and osax commands don't always play well together. In this case, display dialog with icon always seems to throw an error if any frameworks have been invoked. I suppose I could have used tell framework "..." blocks instead, but the script object appealed to me...

As Ted correctly answered you can't display compound icons.
But you can do the opposite, displaying your application icon with a caution or stop badge.
To do so omit the reference to the icon and use the other – enumerated – with icon parameter
display dialog "this is my icon" buttons {"OK"} default button "OK" with icon caution
display dialog "this is my icon" buttons {"OK"} default button "OK" with icon stop

Related

applescript to split multipage tiff

I'm trying to make a pdf of a multipage tiff file.
I managed to do that in windows with image magick.
In a project we are working together with a macbook with OSX (new to me).
And there should it be possible to.
Is there a script/automator... to do this.
Now I split the multipage tiff (2pages) in preview manually: open the tiff, drag each page to a folder and than I run a script to make a pdf.
Can that be done in a script?
I greet every user who tries to do without third-party applications where possible. The question asked by the OP is much more interesting than many of the trivial questions popping up in this section. Moreover, I do not understand the minuses put down by two users to the question asked. Here's the solution:
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "QuartzCore"
use framework "Quartz"
use framework "AppKit"
property |NSURL| : a reference to current application's |NSURL|
property NSString : a reference to current application's NSString
property PDFPage : a reference to current application's PDFPage
property NSImage : a reference to current application's NSImage
property PDFDocument : a reference to current application's PDFDocument
property NSBitmapImageRep : a reference to current application's NSBitmapImageRep
-- select TIFF
set anAlias to choose file of type {"public.tiff"} with prompt "Select Multi-page tiff file"
-- get selected TIFF's name and base name
tell application "Finder"
set pathString to NSString's stringWithString:(name of anAlias)
set baseName to (pathString's stringByDeletingPathExtension()) as text
end tell
-- make new destination folder (if it doesn't exist already)
tell application "Finder"
try
set destinationFolder to (make new folder at desktop with properties {name:baseName}) as text
on error
set destinationFolder to "" & (path to desktop folder) & baseName
end try
end tell
set destinationFolder to POSIX path of destinationFolder
--Read Multi-Page TIFF
set aURL to |NSURL|'s fileURLWithPath:(POSIX path of anAlias)
set aImage to NSImage's alloc()'s initWithContentsOfURL:aURL
set aRawimg to aImage's TIFFRepresentation()
set eachTiffPages to (NSBitmapImageRep's imageRepsWithData:aRawimg) as list
-- extract each tiff page as PDF file
set pageNum to 1
repeat with curPage in eachTiffPages
set thisImage to contents of curPage
set aImg to (NSImage's alloc()'s initWithSize:(thisImage's |size|()))
(aImg's addRepresentation:thisImage)
--Make Blank PDF
set aPDFdoc to PDFDocument's alloc()'s init()
-- set PDF first page's content to next image
(aPDFdoc's insertPage:(PDFPage's alloc()'s initWithImage:aImg) atIndex:0)
-- write PDF to destination
set outPutPath to destinationFolder & "/Page_" & pageNum & ".pdf"
(aPDFdoc's writeToFile:outPutPath)
set pageNum to pageNum + 1
end repeat

Move all files / folder within SmartFinderFolder to destination w/ serial / single consecutive operation with AppleScript MacOS Serria

Objective: Move all files and files in folders to destination folder and maintain the file structure [files and named folders]. Important for music files in albums.
Functional: Move all listed files in SmartFolder [named] to destinationFolder with serial / consecutive move operation and maintain the same file structure and copy of dataFiles listed in SmartFolder.
Key: All files were obtained for transfer. Normal CMD + A, CMD + C, CMD + V hangs up the computer and the transfer does not initiate. The AppleScript to move each dataObject to destinationPath is all.
Facts: How to reference objects [files, folders] and their proper reference format and accepted path syntax; path or POSIX, and use of alias. Basic operations. I ran an AppleScript to move filePath to pathDestination, and was otherwise successful, and would be nice to known the formalization syntax for path reference.
tell application "Finder"
move allFiles to destinationFolder
// recursive/repeat code to loop through all listed files and folder
end tell
Reference: Applescript, show all files with tag
[Moving / selecting listed files from 'smartfolder' containers as active windows and displayLists. It was an alt. solution since AppleScript will not reference the SmartFolder as an object, nor will it dynamically call the listProperty of the SmartFolder object unless called by an unknown or un-reference method or command.
Since your main issue, as far as I can tell, seems to be dealing with SmartFolders in AppleScript, which—as you said—cannot be referenced as folder objects, this little snippet might be of help:
set SmartFolder to "/Users/CK/My Smart Folder.savedSearch"
tell application "System Events" to get value of property list file SmartFolder
set {[Scope], Query} to {SearchScopes, RawQuery} of RawQueryDict of result
set AppleScript's text item delimiters to tab
set Command to {"mdfind -onlyin", ¬
quoted form of Scope as text, ¬
quoted form of Query as text} as text
set SearchResults to paragraphs of (do shell script Command)
--> returns a list of posix files
--> e.g. {"/Users/CK/Downloads/This is file one.txt", ...}
This will return a list of POSIX paths to files that match the search criteria.
I would recommend using System Events rather than Finder to deal with large numbers of files. It can also handle posix paths without the need to manually coerce them into aliases or whatever. So, given a posix path, such as the ones returned in the list using the above code snippet, you simply do this:
set myfile to item 1 of SearchResults
tell application "System Events" to move myfile to "/Users/CK/Desktop"
Without knowing more details of what your smart folder contains (since some searches could easily return a folder plus the contents of that folder, which you'd have to bear in mind when getting your AppleScript to recurse through the search results), I can't give you more than that. But, you said your main problems were being unable to handle SmartFolders and not knowing how to reference files/folders.
This works for me using the latest version of Sierra.
Set the value of property moveToNewFolderto the destination folder of your choice
This script creates a "Choose From List" Dialog, allowing you to choose any smart folder which resides on your system. It will then move all of these files and folders in the chosen smart folder, to your set destination folder.
property savedSearches : (path to home folder as string) & "Library" & ":Saved Searches"
property savedSearchesSubFolders : {}
property namesOfSavedSearchesSubFolders : {}
property selectedSearchFolder : ""
property selectedSearchFolderPath : missing value
property moveTheseItems : missing value
property moveToNewFolder : (path to desktop as text) & "untitled folder" -- change this value to your preferred destination folder
tell application "Finder"
activate
delay 0.1 -- may need to adjust delay time value
open savedSearches
delay 0.1 -- may need to adjust delay time value
reveal savedSearches
delay 0.1 -- may need to adjust delay time value
select savedSearches
delay 0.1 -- may need to adjust delay time value
set current view of Finder window 1 to column view
delay 0.1 -- may need to adjust delay time value
tell its Finder window (POSIX path of savedSearches)
delay 0.1 -- may need to adjust delay time value
set savedSearchesSubFolders to items
set namesOfSavedSearchesSubFolders to name of items
-- Allows to choose any smart folder from a list of all smart folders
set selectedSearchFolder to choose from list namesOfSavedSearchesSubFolders ¬
with title ¬
"Smart Search Folders" with prompt ¬
"Choose Your Folder" OK button name ¬
"OK" cancel button name "CANCEL"
set selectedSearchFolder to selectedSearchFolder as text
set selectedSearchFolderPath to savedSearches & ":" & selectedSearchFolder
set selectedSearchFolderPath to selectedSearchFolderPath as string
end tell
delay 0.2 -- may need to adjust delay time value
select selectedSearchFolderPath
tell Finder window 1
set defaultView to current view
set current view to list view
delay 0.1 -- may need to adjust delay time value
set current view to defaultView
delay 0.5 -- may need to adjust delay time value
tell application "System Events"
key code 0 using command down
end tell
end tell
set moveTheseItems to selection
end tell
tell application "Finder"
set resultObject to move moveTheseItems ¬
to moveToNewFolder ¬
with replacing
end tell

Batch resizing images in OSX Automator white background instead of black

I'm using Automator to resize a bunch of images. It works great apart from the way it adds a black background to PNG images. This is a real issue if the image was a black logo with a transparent background. Is there any way to change the background colour?
I'm using the Crop Images action.
Cheers!
Automator is very easy to use but for more finite control, you can use AppleScript (even as part of Automator) with Image Events to do some basic image editing including resizing using the pad command with specific dimensions and background color. Here's a basic script that you can save as an application in Script Editor and then, to use it, either double-click the app icon in the Finder or, better, drop the image files to process on the application icon:
property _width : 400
property _height : 200
property _color : {65528, 65535, 65525} --white
on run
open (choose file with multiple selections allowed)
end run
on open the_items
set output_folder to (((path to desktop) as string) & "Output")
try
get output_folder as alias
on error
tell application "Finder" to make new folder at desktop with properties {name:"Output"}
end try
repeat with this_item in the_items
set this_item to (this_item as alias)
set _info to info for this_item
set _extension to _info's name extension
if (_extension is in {"jpg", "jpeg", "tif", "tiff", "png"}) then
set _name to name of _info
set _name to (text 1 thru -((count _extension) + 1) of _name)
set output_path to (output_folder & ":" & _name & "jpg")
my resize_image(this_item, output_path, _width, _height)
end if
end repeat
end open
on resize_image(original_path, output_path, _width, _height)
tell application "Image Events"
launch
set _image to (open file (original_path as string))
pad _image to dimensions {_width, _height} with pad color _color -- use crop to dimensions to decrease the size
save _image as JPEG in file output_path with icon
close _image
end tell
end resize_image
This will create a folder on your Desktop named Output (if one does not already exist) and save the images as JPGs there using the dimensions at the top of the script (you can modify the dimensions, color, output location, etc., this is just an example).

Select the first image in a folder selected in the Fiinder

I have a script that changes the icon of a folder with the image you select and it works fine but I want to be able to have the script automatically select the first image of the folder and assign that as the image that will be used to create the icon.
This is what I have so far. It is unable to select the image and gives various error messages depending on what I try. Any help would be greatly appreciated!
set exePath to (path to home folder as text) & "downloads:SetFileIcon"
tell application "Finder"
set theSelection to selection -- This is a list of selected items, even if only 1 item is selected.
set theFile to (item 1 of theSelection) as text -- Get the first item in the list. We make it text so we can convert it to a posix path for SetFileIcon.
set theimage to item 1 of theSelection
set imagePath to (path to desktop folder as text) and theimage
end tell
do shell script quoted form of POSIX path of exePath & " -image " & quoted form of POSIX path of imagePath & " -file " & quoted form of POSIX path of theFile
A couple of issues with your script at first glance. First, you’re never asking for the contents of the folder. You set theFile to item 1 of theSelection and then you set theimage to item 1 of theSelection. I expect you meant to set theimage to item 1 of the document files of theFile. Note that theFile is actually the selected folder.
Second, “and” is a boolean operation; to concatenate text in AppleScript, use “ & ”. Thus, even if theimage contained the image’s name, you would need to use & theimage.
However, since you are asking the Finder for the information, the Finder already knows the full path to any particular file. So, once you get an image, you can ask the Finder for the path directly.
tell application "Finder"
-- This is a list of selected items, even if only 1 item is selected.
set theSelections to the selection
-- This code assumes that the first selected item is a folder
set theFolder to the first item of theSelections
-- we want the first document that is also an image
set theImages to the document files of theFolder whose kind contains "image"
set theImage to item 1 of theImages as text
-- and we want it's path
set imagePath to POSIX path of theImage
end tell

AppleScript: Organize images based on image dimensions

I'm brand new to AppleScript and I'm trying to write a basic script that does the following:
Finds images (PNGs) in the folder ~/Dropbox/Camera Uploads that are exactly 640x1136 (iPhone 5 screenshots) and moves them to ~/Dropbox/Camera Uploads/Screenshots.
This seems pretty straightforward, but so far I haven't been able to figure it out.
Here's how I would do it. I wouldn't worry about performance. I ran the Image Events section on 200 files, and it only took 1 second.
set picFolder to alias "Path:to:Dropbox:Camera Uploads:"
set screenshotFolder to alias "Path:to:Dropbox:Camera Uploads:screenshots:"
tell application "System Events"
set photos to path of files of picFolder whose kind is "Portable Network Graphics image"
end tell
set screenshots to {}
repeat with imgPath in photos
set imgAlias to alias imgPath
tell application "Image Events"
set img to open imgPath
if dimensions of img = {640, 1136} then
set end of screenshots to imgAlias
end if
close img
end tell
end repeat
tell application "Finder"
move screenshots to screenshotFolder
end tell
You need to have an AppleScript-aware application that can act based on the dimensions of an image file. I don’t think the Finder can do this, despite its ability to show the dimensions of images in Finder views.
iPhoto should be able to do this. The iPhoto dictionary indicates that “photos” have both the width and height of images. So you should be able to write an AppleScript that imports them into iPhoto first, then selects those that match your criteria, and then saves them to the appropriate Dropbox folder.
Depending on your needs, you might also look at Automator. It contains iPhoto actions as well, including one to “Filter iPhoto items”. If you create a Folder Action you should be able to create an Automator script that starts up whenever something new is added to your Camera Uploads folder, adds them to iPhoto, and then copies them to your Screenshots folder.
If nothing else, you should be able to use Image Events to get all images in the folder, and then act only on the ones that match your criteria. Something like:
tell application "Image Events"
tell folder "Macintosh HD:Users:colin:Dropbox:Camera Uploads"
copy (files where kind is "JPEG image") to potentialScreenshots
repeat with potentialFile in potentialScreenshots
set potentialScreenshot to open potentialFile
set imageDimensions to dimensions of potentialScreenshot
if item 1 of imageDimensions is 640 then
set fileName to name of potentialFile
tell me to display dialog fileName
end if
end repeat
end tell
end tell
There ought to be a way to tell Image Events to only look at files whose dimensions match what you want, but I can’t see it.
Try:
set folderPath to POSIX path of (path to home folder) & "Dropbox/Camera Uploads"
set screenshotsPath to POSIX path of (path to home folder) & "Dropbox/Camera Uploads/Screenshots"
try
do shell script "mdfind -0 -onlyin " & quoted form of folderPath & " \"kMDItemPixelWidth == 640 && kMDItemPixelHeight == 1136\" | xargs -0 -I {} mv {} " & quoted form of screenshotsPath
end try

Resources