I'm trying to render 50k images using a Mac Mini. I don't know much about the system, but I need to use the built-in renderer like the Preview.app.
I currently tested to set up an Automator script that could take files selected in the Finder window, render images, and move them to another directory.
But running that script with 50k single-page PDFs does not work. Instead, it seems to use the memory to store the PDFs or something like that.
I have never written any AppleScript, Swift, or similar code, but I'm well versed in Bash and Java, so if I could do this from the command line, then it would be ideal, but if someone has a suggestion on an AppleScript that could solve this issue I'm all ears.
Thank you for reading this.
-- 1) Create --> **Quick Action** (that is, service)
-- 2) Set: **Workflow receives current PDFs in Finder.app**
-- 3) Add action **Run AppleScript** with following content.
-- 4) Save this service with name like "Render images from single_page PDFs".
use scripting additions
use framework "Foundation"
use framework "AppKit"
use framework "Quartz"
use framework "QuartzCore"
on run {input, parameters}
set destinationFolder to (choose folder with prompt "Please, choose Destination Folder")
set destFolderPosixPath to POSIX path of destinationFolder
repeat with aPDF in (get input)
(my rasterPDF:aPDF savingToFolder:destFolderPosixPath)
end repeat
return input
end run
on rasterPDF:aPDF savingToFolder:destFolderPosixPath
set aURL to (|NSURL|'s fileURLWithPath:(POSIX path of aPDF))
set fileName to aURL's lastPathComponent()
set baseName to fileName's stringByDeletingPathExtension()
set aPDFdoc to PDFDocument's alloc()'s initWithURL:aURL
set doc to (NSImage's alloc()'s initWithData:((aPDFdoc's pageAtIndex:0)'s dataRepresentation()))
if doc = missing value then error "Error in getting image from PDF"
set theData to doc's TIFFRepresentation()
set aNSBitmapImageRep to (NSBitmapImageRep's imageRepsWithData:theData)'s objectAtIndex:0
set outNSURL to |NSURL|'s fileURLWithPath:(destFolderPosixPath & baseName & ".png")
set outputPNGData to aNSBitmapImageRep's representationUsingType:NSPNGFileType |properties|:{NSImageCompressionFactor:0.8, NSImageProgressive:false}
outputPNGData's writeToURL:outNSURL atomically:true
end rasterPDF:savingToFolder:
NOTE: 1) the script renders image from first page of multiple_page PDFs as well. 2) with using repeat loop and with changing pageAtIndex property, someone can adapt my script to render images from all PDF pages.
Related
I have a weird issue with mac automator (on Mojave).
What I want: Take a pdf > save each page as png to same folder as pdf
What happens: Takes a pdf > all pngs are saved to desktop
Here is the automator after a test run:
You can see on the screenshot how variable "pdfPath" actually is set correctly to Test folder. But then after moving it does remove the files out of system folder, but not to pdfPath but to Desktop. Important: if I manually pick any folder, it will save it there and not to desktop, so still somehow related to the pdfPath variable?
Anybody an idea why it happens and how to fix?
Automator just gets frustrating like this at times. The Move Finder Items action doesn’t appear to work with variables in this situation, even though you can drag and select them - when using a variable it defaults to the Desktop. As a workaround you can replace the Move Finder Items action with a Get Value of Variable for pdfPath to add it to the input items, then add a Run AppleScript action to do the moving:
on run {input, parameters}
if (count input) < 2 then error "No Items to move.“
set destination to last item of input
tell application "Finder" to move (items 1 thru -2 of input) to destination
end run
You might also have to clean out previous results from the temporary folder to keep the rename action happy.
Ok, I figured it out with some help in the meantime. For anybody coming across and looking for solution, the path I saved in the Applescript part was showing up nicely in the result, but in the end, the path was in the wrong format. Following code works like a charm:
on run {input, parameters}
tell application "Finder" to return POSIX path of (container of (item 1 of input) as alias) as text
end run
The following command will run the SCRIPT_ABC.jsx which is located on my desktop. Is there a way to take the contents of the SCRIPT_ABC.jsx and put the javascript extendscript exclusively inside automator so when I save it out as an application it will be included within the program?
on run {input, parameters}
tell application "Adobe Illustrator" to open file "Macintosh HD:Users:NAME:Desktop:SCRIPT_ABC.jsx"
return input
end run
Paste the entire jsx into your applescript as a text body, then use the do javascript command to run it. Applescript has a great benefit of being able to pass in arguments to the jsx (do javascript myScript with arguments { textVariableHere, anotherOneHere }) and obtain the returned result too!
Elaborated edit:
set myJavascript to "
#target illustrator
function test(args){
var docName = app.activeDocument.name;
alert(\"Hello from Adobe JSX. Current document name is: '\" + docName + \"'\\n\" + args[0]);
return docName;
};
test(arguments);
"
set myInputVar to "The input string here."
tell application "Adobe Illustrator"
set myResult to do javascript myJavascript with arguments {myInputVar}
end tell
display alert ("Returned Document Name is: '" & myResult & "'")
So what you see here is an example of a javascript embedded inside the applescript as a string. Lucky for me, I have AppleScript Debugger app which I paid for a long time ago and is well-worth it, and it has the "Paste as string literal" function which auto-escapes the quotes and stuff. Very helpful.
But you can see the magic of passing in a variable to the javascript as applescript arguments which prevents a lot of issues one could have doing string-building inside applescript to put variables in.
Although you don't have to use AppleScript arguments to put data into the jsx, as you can use the string-building mentioned above (which I do not recommend when the arguments way is available for use), the huge payoff is actually getting the returned jsx data back into the applescript. Really neat stuff.
So there you have it, the whole enchilada! I would be curious to see if there's a comparable native way to do a similar setup with Windows and VBS + JSX combo - if anyone is aware of something like this please let me know in the comments below.
Thanks!
PS:
The word arguments used in the function call of the main function of the jsx script is actually a key-word used by AppleScript and it has to be spelled out like so, otherwise it will not work.
More Edit:
Okay, so maybe you don't wanna paste no huge jsx into your applescript and escape all the quotes, etc.
I get it. No worries! You can do the following:
Ensure your jsx is somewhere installed along with you app (it could be binary), so save the following code which represents your jsx body as a jsx file wherever:
var docName = app.activeDocument.name;
alert("Hello from Adobe JSX. Current document name is: '" + docName + "'\n" + args[0]);
return docName;
And in your AppleScript you simply change the jsx portion to the following:
set myJavascript to "
#target illustrator
function test(args){
#include '/Users/YourName/Wherever/myAppleScriptJsxTest.jsx'
};
test(arguments);
"
BOOM! Works exactly the same! Isn't that something?
You can copy the .jsx file to the Contents/Resources folder of your Automator application bundle, and use a Run AppleScript action to get the resource
on run {input, parameters}
try
set thePath to path for resource "SCRIPT_ABC" extension "jsx"
tell application "Adobe Illustrator" to open (thePath as POSIX file)
on error errmess
display dialog errmess
end try
return input
end run
Note: You will need to copy the file in question again to the bundle /Contents/Resource folder when you recreate your application bundle since Automator creates a fresh bundle each time you save it.
Also, it may be required to close Automator/the Script Editor after you have added the file. Finally, test the resulting app and see if it launches the file in the requested app.
Here is the AppleScript that executes ~/script.jsx (there can be any jsx script) via Adobe Illustrator:
tell application "Adobe Illustrator"
do javascript "(function(){//#include '~/script.jsx'}());"
end tell
Or you can set Adobe Illustrator to open all jsx files by default. After that a double click on any jsx file will make Illustrator to execute the script. It works on Windows and MacOS.
I'm creating an automator pdf print plugin.
When you choose the print plugin the filename to the pdf is the input (normally /var/something /documentName.pdf)
I would like to get the documentName to use it later in an Rename Finder Item.
I'm using atm applescript to accomplish this.
on run {input, parameters}
tell application "Finder"
set fileName to name of ((POSIX file input) as alias)
end tell
return fileName as string
end run
The problem is that this only works when I put an Ask for Text Action before the applescript which displays the posix path.
If I remove the Ask for Text action the applescript fails.
The workflow is at https://www.dropbox.com/s/jp4t9pen3gvtyiq/Rename-Action.workflow.zip
I guess it is something simple but this is the first applescript / automator workflow I'm creating.
As I fail on commenting
Solution is
on run {input, parameters}
tell application "Finder"
set fileName to ((name of first item of input) as string)
end tell
return fileName
end run
as by #Ken post below.
Thanks!
I created a test workflow with this AppleScript:
on run {input, parameters}
tell app "System Events"
display dialog ((class of input) as string)
end
return input
end run
That displayed "list". I then modified it to:
on run {input, parameters}
tell application "System Events"
display dialog ((class of first item of input) as string)
end tell
return input
end run
That displayed "alias".
So, the input to a PDF workflow is a list of aliases. Write your script with that in mind and it should work. For example, this works:
on run {input, parameters}
tell application "System Events"
display dialog ((name of first item of input) as string)
end tell
return input
end run
When working with AppleScript, it can really help to forget everything you know about file paths. If you think in paths, you will be doing path-math in your head and all of it is unnecessary work. What you want to work with is objects. When doing file operations, you work with alias objects.
If you look at the PDF you are working with in Finder, and go File ▶ Make Alias then you’ll create an alias file. You can drag that alias file all around the file system of the disk it is on, put it in any folder, and when you open the alias file, it will still always open your original PDF, even if you forget what path name your original PDF file has, and even more importantly: the alias will open the PDF even if the PDF has moved to somewhere else in the file system. An alias does all that work for you. You don’t need to know the path names.
In AppleScript, rather than working with files, you work with aliases, and whatever you do to an alias is also done to the original file. So you don’t need to know the path name of a file to change its name — you only have to have an alias of it to work on. You store that alias in a variable.
So what you want to do is set the input PDF alias to a variable, and then later, that variable is what you give Finder to rename. You don’t have to know any paths. It doesn’t matter where the input PDF is stored on the file system — the alias will take care of that.
Here is an example AppleScript that demonstrates the principle of taking an alias as input, and then later renaming that alias (and thus, the original file:)
tell application "Finder"
set theInputFile to (choose file)
-- do a workflow here
set the name of theInputFile to "Renamed" & "." & the name extension of theInputFile
end tell
Here is a line-by-line description of the above script:
the opening tell block that specifies we are talking to Finder
show the user a choose file dialog box, and set the alias that is returned by that dialog box to a variable called “theInputFile”
a comment that is a placeholder for whatever workflow steps you might want to do
rename theInputFile to “Renamed” and its original file extension
quit talking to Finder
And even where you want to work with the folder that contains your original input file, or want to know what disk the input file is on, you still don’t need to work with path names:
tell application "Finder"
set theInputFile to (choose file)
set theContainingFolder to open the container of theInputFile
set theInputFileDisk to the disk of theInputFile
end tell
And if you want to know what kind of file the input file is, you don’t have to look at the filename extension and figure it out, you can just say:
set theInputFileKind to the kind of theInputFile
if theInputFileKind is equal to "Portable Document Format (PDF)" then
-- do stuff
end if
And if you want to work in specific folders, such as the home folder, there are special properties for that, like “the path to the home folder” so that the following script opens “~/Public/Drop Box” on any system, no matter what the user name:
tell application "Finder"
activate
set theHomeFolder to the path to the home folder as alias
set theDropBoxFolder to folder "Drop Box" of folder "Public" of theHomeFolder
open theDropBoxFolder
end tell
You can walk around disks and folder structures as objects as shown above, so again, there is no need to think in paths. Think in terms of setting variables to objects that you want to interact with.
Solution is
on run {input, parameters}
tell application "Finder"
set fileName to ((name of first item of input) as string)
end tell
return fileName
end run
as by #Ken's post
Thanks!
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
I want AppleScript to loop through a set of of RTF files in folder and save them as HTML files.
This is my simple code so far. The XXXX is where I'm struggling:
tell application "Finder"
set source_folder to choose folder
set aList to every file in source_folder
repeat with i from 1 to number of items in aList
tell application "TextEdit"
set aFile to (item i of aList)
save as aFile XXXXXXXXX
end tell
end repeat
end tell
I'm really new to this... any help much appreciated.
You don't need TextEdit for this. There is a command line program textutil which will do the job without all the opening and saving stuff required with TextEdit. We can fix your TextEdit script (it has a few errors) but try this first and let us know if it does the job for you. The html files will have the same name but with the html extension and will be located in source_folder. The ouput path can be changed in the code by using the "-output" switch of textutil. See "man textutil" if you want to look at everything it can do.
And a general question... what is a RTD file? Do you mean rtf or rtfd? Textutil will work with rtf/rtfd but not rtd, so I hope that isn't really your file type.
set source_folder to choose folder with prompt "Choose a source folder."
set output_folder to choose folder with prompt "Choose an output folder."
tell application "Finder"
set theFiles to (files of entire contents of source_folder) as alias list
end tell
repeat with aFile in theFiles
tell application "Finder"
set fileName to name of aFile
set fileExt to name extension of aFile
end tell
set outputPath to (output_folder as text) & text 1 thru -((count of fileExt) + 1) of fileName & "html"
do shell script "/usr/bin/textutil -convert html -output " & quoted form of POSIX path of outputPath & space & quoted form of POSIX path of aFile
end repeat
You mention you are new to applescript, so I'll give you some general pointers you should keep in mind when writing applescript code.
Avoid putting tell blocks of code inside each other. You have tell app TextEdit inside tell app Finder. That's bad. Doing this is a source of many conflicts because you are basically telling the Finder to tell TextEdit to do something. That's not good because commands can get confused and it's really hard to debug these kinds of issues. So keep your tell blocks separate.
Avoid telling an application to perform a command that is not in its applescript dictionary. You should only tell an application to do commands that it knows and an application only knows about the commands in its dictionary. So for example, you are telling the Finder to "choose folder". The Finder does not know that command. That's an applescript command. So doing as you have done is another possible source of errors. In this case that's a simple command and it will work but in general avoid doing this.
Regarding the Finder, you should avoid using it too much. The Finder is a major program on your computer and is often busy doing computer related stuff. As such it's best to only use it when necessary. As an example you can see in my code that I removed the "choose folder" and the repeat loop from the Finder. I purposely appended "as alias list" to the end of the Finder command to make the list of files usable outside of the Finder tell block of code. Of course use the Finder if needed but it's best to not use it if you don't need it.
Use the applescript dictionary of your applications. As mentioned above, the dictionary lists all of the terms and the syntax that an application understands (granted the dictionaries are difficult to understand but you will get better at it the more you use them). Under the file menu of AppleScript Editor choose "Open dictionary" and a list of all the applications that understand applescript is shown. Choose an application from that to see its dictionary. So for example, you are trying to figure out TextEdit's "save as" command. You can usually get good direction from the dictionary so you should take a look at that. Use the search field to search!
So I hope that helps! Good luck.