Automated importing/sizing of image illustrator - applescript

I’m trying to find a way to place an image into an illustrator document with AppleScript, but I can’t find a place image command that works.
Ideally I would give the AppleScript my image’s file path, and it would place said image in a file when executed.
Anybody do this before?

Do you have the Adobe Illustrator CC 2017 Reference PDF? This example is straight out of it:
-- This function adds a new placed item to a document from a file reference,
-- fileToPlace, which is passed in during the function call, fileToPlace is an
-- alias or file reference to an art file, which must be set up before calling this
-- function, itemPosition is a fixed point at which to position the placed item
on PlacedItemCreate(fileToPlace)
tell application "Adobe Illustrator"
set itemPosition to {100.0, 200.0}
set placedRef to make new placed item in document 1 ¬
with properties {file path:fileToPlace, position:itemPosition}
end tell
end PlacedItemCreate

Related

MacOS render images from PDFs (no third party render lib)

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.

Get path to original item of a file alias when it's missing

I'm trying to write a script to correct the path to an original item of a file alias. I know what's wrong with the path (I moved a folder), so if I can get what the system thinks the path is, I can fix it.
The problem is that the script simply reports that the original can't be found and not where the original should be.
Is there a way to grab that from with AppleScript? It's there in the Get Info... box, so it's stored in the file. I suspect I could use some bash way to get it with a do shell script, but I'm curious about staying within AS.
Years ago, I also had issue with my alias, when changing folders. This is part of the script I made which looks into Get Info window. Since, may be the Get Info window has been changed on last OS version, therefore, some adjustments are required, but it gives you the direction to go:
on Origine_Alias(F_Alias)
set R to ""
tell application "Finder" to open information window of file (F_Alias as text)
tell application "System Events" to tell process "Finder" to set R to value of static text 18 of scroll area 1 of front window
tell application "Finder" to close front window
return R
end Origine_Alias
The returned string is in Unix format: /folder/my/file

Run illustrator extendscript through automator applescript or bash?

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.

Applescript Droplet - opening a file in Photoshop CS6

I'm creating a droplet with Applescript which will basically open the file in photoshop CS6 and then process it.
So far I can't get past the opening the file bit. Can someone identify whats wrong with this code:
on open {the_PDFs}
set myFilePath to the_PDFs
tell application "Adobe Photoshop CS6"
open myFilePath as PDF with options {class:PDF open options, mode:RGB, resolution:300, page:x, constrain proportions:false}
end tell
end open
I keep getting an error saying "Can't get Alias "Macintosh HD:Users:MyName:Desktop:myFile.pdf"
ok so a few things
not sure why you are using brackets but you need to remove those
next the_PDFs is an array of files no a single file so you must loop through them
last you tell it to open page x but never define x
on open the_PDFs
set x to 1
repeat with afile in the_PDFs
tell application "Adobe Photoshop CS5.1"
open afile as PDF with options {class:PDF open options, mode:RGB, resolution:300, page:x, constrain proportions:false}
end tell
end repeat
end open
NOTE: I do not own CS6 so i could not test in CS6 though there shouldn't be any change in this
Put away the brackets from the first line, it should work:
on open the_PDFs

Changing the path to a file in iTunes with AppleScript

I am trying to write an iTunes script that takes the selected tracks, moves the files to a different folder on my hard drive, and then update their location in iTunes.
The overall flow will be something like this:
Get selection
Determine path to selection
Move items to destination
Update referenced path in iTunes
I used the suggestion from this question to get the path of a selection in iTunes, and I will be able to figure out how to move the files to where I want them, however, now I want to tell iTunes that the file path is actually some place else.
Anybody know how to go about this?
I figured it out. I had a different error that was making me think this is harder then it is. Here's how I got it to work:
tell application "iTunes"
set s to selection
repeat with c in s
if video kind of c is TV show then
set location of c to <destination directory>
<code to move file>
end if
end tell
The basic idea is to set the location property of each file track item to its new file path. For example:
tell application "iTunes"
tell its first browser window
set currentTrack to first item of (get its selection)
set location of currentTrack to POSIX file "/Users/nad/Music/movedfile.m4a"
end tell
end tell

Resources