Create PDF from selected images using automator service - bash

I found many topics on that but none of the solutions seem to fit what i want to do or they don't work.
I want to select multiple images in a folder in a finder window.
I want to right-click the selection and run a Service to create a PDF using those images in the same folder those images are in.
I don't know how to get the directory the images are placed in.

Assuming that your selected file could be from different folders, you must loop to each image and get parent folder as bellow :
tell application "Finder"
set myFIles to selection -- read your selection of images
repeat with aFile in myFIles -- loop for each selecgted image
set ParentFolder to container of aFile -- get folder which contains that image
end repeat
end tell

Related

How can I move each folder on my desktop iteratively with AppleScript?

I am trying to write an AppleScript that iterates through the folders in my Desktop folder and moves them to a subfolder called "destination". (I need to iterate through the Desktop folders instead of moving them all at once because I want each folder to be moved or not moved based on a randomly generated number).
My code:
set desktopFolders to (path to desktop as text)
set destinationFolder to (path to desktop as text) & "destination"
tell application "System Events"
set subFolders to (get every disk item of folder desktopFolders)
repeat with eachFolder in subFolders
move eachFolder to destinationFolder
end repeat
end tell
The script works if I try to move the entire subFolders variable into destinationFolder, and it doesn't throw an error from trying to iterate through eachFolder in subFolders. However, when I try to move eachFolder into destinationFolder, I get the following error:
System Events got an error: NSArgumentsWrongScriptError
Any reason the eachFolder variable isn't able to be moved in this way? Is there a better way to iterate through the folders in my Desktop folder, such that I can move each one of them individually?
Change desktopFolders to: desktopFolder
Note there is no s on the end, as the Desktop folder is singular not plural.
Change disk item in:
set subFolders to (get every disk item of folder desktopFolder)
To: folder
Also add:
whose name is not "destination"
To the end of:
get every folder of folder desktopFolder
Example:
set subFolders to (get every folder of folder desktopFolder whose name is not "destination")
You do not want to try moving the destination folder into itself.
Sans making desktopFolders singular, these other changes allow the rest of the code to work without error while testing under macOS High Sierra.

How can I automate a keynote workflow to flatten slides into image slides for export to .ppt

I always design my presentation slides in keynote (because I find it easier and more pleasant to work with), though they often need to be presented on a windows machine running PowerPoint.
In order to avoid issues with fonts, formatting, etc., I always use the following effective workflow:
Design the slides in keynote, often using images and text.
Export the slides as jpg files to a folder on the desktop.
Open a new keynote presentation.
Drag the jpg files into the slide navigator. This creates an image slide of each jpg.
export the new presentation to a .ppt file.
Is there a way I can automate this workflow? I'd love to collapse steps 2-5 into a single step!
Here's the AppleScript that does this (work on Keynote version 6.2, not on version 5):
tell application "Finder" to set f to (make new folder) as text -- create a temp folder to export images
tell application "Keynote"
tell front document
export to (file f) as slide images with properties {image format:JPEG, compression factor:95}
set {h, w, fPath} to {height, width, file of it}
end tell
tell (fPath as string) to if it ends with ".key:" then
set newFile to (text 1 thru -6) & ".ppt"
else
set newFile to it & ".ppt"
end if
set jpegs to my getImages(f)
set newDoc to make new document with properties {width:w, height:h}
tell newDoc
set mSlide to last master slide -- blank
repeat with thisJPEG in jpegs
set s to make new slide with properties {base slide:mSlide}
tell s to make new image with properties {file:thisJPEG}
end repeat
delete slide 1
export to (file newFile) as Microsoft PowerPoint
close saving no
end tell
end tell
tell application "Finder" to delete folder f -- delete the temp folder
on getImages(f)
tell application "Finder" to return (files of folder f) as alias list
end getImages
Important:
the slideshow must be already open in Keynote before running the script.
And the slideshow must be already saved, because the script use the path of the front document to save the PPT file in the same folder.
--
Updated: to choose location of the new file
set v to ("Volumes" as POSIX file) as alias
tell application "Finder" to set f to (make new folder) as text -- create a temp folder to export images
tell application "Keynote"
tell front document
export to (file f) as slide images with properties {image format:JPEG, compression factor:95}
set {h, w, tName} to {height, width, name of it}
end tell
tell tName to if it ends with ".key" then
set newName to (text 1 thru -5) & ".ppt"
else
set newName to it & ".ppt"
end if
set jpegs to my getImages(f)
activate
set newFile to choose file name default name newName default location v with prompt "Select the folder to save the PPT file"
set newDoc to make new document with properties {width:w, height:h}
tell newDoc
set mSlide to last master slide -- blank
repeat with thisJPEG in jpegs
set s to make new slide with properties {base slide:mSlide}
tell s to make new image with properties {file:thisJPEG}
end repeat
delete slide 1
export to (newFile) as Microsoft PowerPoint
close saving no
end tell
end tell
tell application "Finder" to delete folder f -- delete the temp folder
on getImages(f)
tell application "Finder" to return (files of folder f) as alias list
end getImages

How to remove alpha from bulk images with AppleScript

I have a bulk amount of images, of which i need to remove alpha from each. This is doable from Preview application, but the sheer amount of times i would need to repeat that is way too time consuming.
I have heard about AppleScript and made some feeble attempts at automating the process, currently to no avail.
i am using something like this and then starting a repeat, but it only allows me to loop through one direct folder (also i'm having troubles with menu bar items)
set fl to files of folder POSIX file "/Users/user/Documents/" as alias list
But i have multiple folders within folders which i wish to change an amount of images. the folder structure is like this:
Users/user/folder/ImagesRoot/
in inside ImagesRoot are 3 folders and one txt file. I want to specifically select 2 of the folders called "Icons" and "Screenshots". In "Icons" is 5 images. However "Screenshots" contains 3 subfolders, each with their own 5 images. (can be referred to as "Screensub 1,2,3")
Once receiving the list of images inside such folder, the process would be something like
tell application "Preview"
open the image file
open the file menu
open export...
untick alpha
press save
press replace
close window
end tell
Loop to next one
when looped through all in Icons folder, do all 5 images of each subfolder of screenshot folder
I am told that AppleScript is a good way to do this, but also that bash is a possibility?
However, i have 2% experience with applescript and maybe 4% of bash, and don't know how to approach it.
Any advice or help would be greatly appreciated.
Thanks
As far as I can tell, Preview doesn’t have the ability to change the alpha of a document, even if you enable AppleScripting. In an application that does support it, such as GraphicConverter, this is a fairly simple task:
on open imageFile
tell application "GraphicConverter 9"
open imageFile
tell window 1
set alpha to false
end tell
--save image as needed
end tell
end open
You may find it easier to use Automator. The combination of Get Folder Contents with Repeat for each subfolder found and Alpha Channel set to Remove may be all you need. If you need to exclude some files, Filter Finder Items would do it.
If you want to use Preview, however, you may be able to use System Events. Something like:
on open imageFile
tell application "Preview"
open imageFile
activate
set filename to name of window 1
tell application "System Events"
tell process "Preview"
click menu item "Export…" of menu "File" of menu bar 1
tell sheet 1 of window 1
--rename file
set value of text field 1 to filename & " Alpha Removed"
--uncheck alpha channel
tell checkbox 1 of group 1
click
end tell
--save
click button "Save"
end tell
end tell
end tell
end tell
end open
If you choose the assistive access route, you may find Apple’s help on signing applications useful: http://support.apple.com/kb/HT5914

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

AppleScript or Automator flow to import photos into iphoto and set album name as files folder name

I store photo files in folders on my computer before i add them into iphoto.
I want to add the contents of a selected to folder to iphoto and name the new album as the folder name
I have created an automator flow where I do the following
(Select folder initially to work)
Get selected finder items -- This is grabbing the folder
Set Value of Variable -- this is grabbing the full path name and setting a variable with the name
Get Folder contents -- this gets all the photos contained.
Import Files into iphoto -- This adds the photos into iphoto and creates a new album using the variable name.
The issue i have is the variable name sets the full path of the files,
/Users/Johnny/Photos/Dayout
Is the a script that can take just the name of the initial folder "Dayout"
Thanks in advance to anyone who can help
Cheers
John
activate application "SystemUIServer" -- http://www.openradar.me/9406282
tell application "Finder"
activate
repeat with f in (get selection as alias list)
set n to name of f
tell application "iPhoto"
if not (exists album n) then new album name n
import from f to album n
end tell
end repeat
end tell
There is a bug in 10.7 and 10.8 where Finder ignores new windows when getting the selection property. If you open a new Finder window, select some items, and run tell app "Finder" to selection in AppleScript Editor, the result is items selected in some window behind the frontmost window (or an empty list). One workaround is to move focus to another application and back.

Resources