Applescript Droplet - opening a file in Photoshop CS6 - applescript

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

Related

AppleScript to Crop Images by Path Selection Without Deleting Pixels in Photoshop

I need a little help about an AppleScript script which for a folder of pictures :
Make a Selection out of already created path named Path 1
Then Crops the picture by that selection without Deleting the Cropped pixels
I have a problem finding the syntax in AppleScript to turn off "Deleting Cropped Pixels"..and looks like there is no such. I am lookin for a solution to save those pixels and a way to implement it in Applescript.
The solution i have uses key code commands and it's pretty inconvenient because "Delete Cropped Pixels" have to turned off manually in Photoshop before runing the script. Any OS notification outside PS will break the script.
tell application "Finder"
set ThePath to choose folder with prompt "Please select folder:"
set pictures_collection to every file of ThePath
end tell
repeat with anItem in pictures_collection
tell application "Adobe Photoshop CS6"
activate
set anItem to anItem as string
open file anItem
do action "Make_A_Selection" from "Make_A_Selection" -- runs an action with just makes the selection
set OpenedPicture to the current document
tell application "System Events"
-- The option "Delete Cropped Pixels" in the PS Crop tool is turned OFF manually so the next code do not erace the pixels
key code 8 -- press "C"
delay 0.5
key code 36 -- press "enter"
delay 0.5
key code 36 -- press "enter"
end tell
save OpenedPicture
close OpenedPicture
end tell
end repeat
return input
end run
So i looked for more efficient way but unfortunately i don't know how to implement the option not to "Delete Cropped Pixels" in the next script:
on run {input}
tell application "Finder"
set ThePath to choose folder with prompt "Please select folder:"
set pictures_collection to every file of ThePath
end tell
repeat with anItem in pictures_collection
tell application "Adobe Photoshop CS6"'s document 1
activate
set anItem to anItem as string
open file anItem
create selection path item 1
tell application "Adobe Photoshop CS6"
tell current document
set theCropBounds to bounds of selection
crop bounds theCropBounds
end tell
end tell
save OpenedPicture
close OpenedPicture
end tell
end repeat
return input
end run
I've looked in photoshop-cs6-applescript-reference.pdf but as far as i understood i can only set coordinates of the crop.
Any help will be appreciated.

AppleScript: encoding arbitrary query-string values in URLs passed to Firefox

I use firefox for a long time as my only browser on Pc or Mac.
In a few words my problem: I want to create a service on mac with automator and
Applescript for quite instant translation using translate.google.com.
What works great with Safari or Chrome (below the 4 or 5 lines of script)
On run {input, parameters}
Tell application "Safari"
activate
try
Open location "https://translate.google.com/#auto/en/" & (the clipboard)
end try
end tell
end run
The same thing (script) does not work at all with Firefox, I try by different ways
To circumvent the impossible problem
On run {input, parameters}
Set theProcess to "Firefox"
Set info to {}
Set y to 0
Set x to 0
Set n to 0
Tell application "Applications / Firefox.app"
activate
Open location "http://translate.google.com/#auto/en/"
end tell
Tell application "System events"
Repeat with theProcess in (process "Firefox")
try
Set info to info & (value of (first attribute whose name is "AXWindows") of theProcess)
end try
end repeats
Set n to count of info
info
end tell
Tell application "System Events" to tell process "Firefox"
Set x to "1"
Set frontmost to true
try
Click menu item "Paste" of menu "Edit" of menu bar 1
end try
Repeat while x is "1" -
If x is "1" then
Keystroke "V" using command down
Set x to "0"
end if
end repeat
end tell
end run
By copying and pasting, the actions take place before the complete loading of the page, even by slowing down the Copy and Paste procedure.
After many observations there is a problem of formatting the text contained in the clipboard with the association of the URL, I improved this but it is not perfect yet.
tell application "Applications/Firefox.app" to activate
tell application "System Events" to tell process "Firefox"
set frontmost to true
set sentence to text of (the clipboard)
set thesentences to paragraphs of sentence
set thenewsentences to thesentences as string
set the clipboard to thenewsentences
keystroke "t" using command down
keystroke "https://translate.google.com/#auto/fr/" & (the clipboard) & return
end tell
Anyway if it works with Safari without modifying anything, the problem is at the Firefox entry, so if you can look at this problem, that would be very useful to us all.
Thank you for your attention.
Thank you for your answers.
Safari and Chrome perform the necessary encoding of reserved characters in the URL for you, but Firefox doesn't.
Therefore, you need to perform the encoding of the query-string value (the text to embed in the URL) explicitly.
The easiest (though not obvious) approach is to use perl, via a shell command, gratefully adapted from here:
# Example input that contains chars. that have special meaning in a URL ('&' and '?')
set the clipboard to "Non, Je ne regrette rien & rien ne va plus?"
# Get text from the clipboard and URL-encode it.
set encodedText to do shell script ¬
"perl -MURI::Escape -lne 'print uri_escape($_)' <<<" & quoted form of (the clipboard)
# Now it's safe to append the encoded text to the URL template.
tell application "Firefox"
activate
open location "https://translate.google.com/#auto/en/" & encodedText
end tell
The above approach works with all three browsers mentioned: Firefox, Safari, and Google Chrome.
Note:
As of (at least) Firefox v50, Firefox opens the URL in in a new tab in the current front window by default.
You can make Firefox open the URL in a new window instead, by unchecking Open new windows in a new tab instead on the General tab of Firefox's preferences.
Note, however, that this is a persistent setting that affects all URLs opened from outside of Firefox.
For an ad-hoc solution for opening in a new window that doesn't rely on changing the setting, see this answer of mine.
Hello below the service Automator with some version it is possible to encounter problems, I modified the script so that it works almost everywhere.
A frequent system error is the permission to applications to control your computer that is handled by the system preferences tab Security and Privacy, the system asks if you allow Firefox, TexEdit and others using this service for its keyboard shortcuts.
Also in Automator create service (to be General (and appear in all applications) no entry (up to Yosemite since El Capitan I saw that with Firefox for example all the services are usable), choose Execute a script Applescript paste the script below divided into 2 script or 1 script only.
on run
set Sn to ""
tell application "System Events"
set Sn to the short name of first process whose frontmost is true --here we look for and find which application to launch the service
tell process Sn
set frontmost to true
try
click menu item "Copy" of menu "Edit" of menu bar 1 -- there is used the Copier of the menu Editing of the application
end try
end tell
end tell
return the clipboard
end run
In the script following the entry is done with the contents of the Clipboard
On run {input}
on run {input}
set input to (the clipboard) -- Here we paste the contents of the Clipboard into a variable
try
set input to do shell script "Perl -MURI :: Escape -lne 'print uri_escape ($ _)' <<< " & quoted form of input --To format the text to make it usable with Firefox and the url of translate.google.com
tell application id "org.mozilla.firefox"
activate
open location "https://translate.google.com/#auto/en/" & input --here since version 50 of Firefox you must open a tab and not a new url window of translate.google.com, with option #auto automatic detection by google of the language, and fr to translate into French (these options are modifiable at will)
end tell
end try
end run -- end of the operation you have a tab open on translate, a text to translate and a translated text

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 "Save As" HTML in TextEdit

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.

How to read a file and open Safari with the read result with Mac (AppleScript)?

I need to open multiple Safari (or open the tab is OK) based on the read result.
For example, if a file has
http://a.com
http://b.com
I want to open a.com and b.com using Safari.
How can I do that with Mac/AppleScript?
Maybe I can run python calling "open -a Safari "http://a.com", but I guess AppleScript is the tool for this kind of job.
not sure about python but this will read a text file and open windows let me see if I can get tabs for you though
set locations to paragraphs of (read (choose file with prompt "Pick text file containing urls"))
repeat with aline in locations
if length of aline is greater than 0 then
tell application "Safari"
make new document at end of documents
set URL of document 1 to aline
end tell
end if
end repeat
EDIT:
Ok this is better and it opens them in tabs of a single window
set locations to paragraphs of (read (choose file with prompt "Pick text file containing urls"))
tell application "Safari"
activate
set adoc to make new document
end tell
repeat with aline in locations
if length of aline is greater than 0 then
tell application "Safari" to make new tab at end of window 1 with properties {URL:aline}
end if
end repeat
New Addtion
this is yet another way based on regulus6633's post in conjunction with mine
set locations to paragraphs of (read (choose file with prompt "Pick text file containing urls"))
repeat with aLocation in locations
tell application "Safari" to open location aLocation
end repeat
If you want it to specifically open the links in Safari then mcgrailm's solution is good. However, you don't need the Finder for the first part so take that code out of the Finder tell block. There's no need to tell the Finder to do something that applescript can do itself.
However, you probably want to open the links in whatever browser is the user's default browser. It may be Safari or Firefox etc. You can do that with the "open location" command. So something like this is probably what you want...
set theFile to choose file with prompt "Pick text file containing urls"
set locations to paragraphs of (read theFile)
repeat with aLocation in locations
try
open location aLocation
end try
end repeat

Resources