Open Photoshop file in same directory as AppleScript - applescript

I am trying to automate label generation from MS Excel into Photoshop using AppleScript. I can get all the data from Excel just fine, however getting that data into a predefined Photoshop document causes problems.
Specifically, I am unable to get Photoshop to open the template file which is located in the same folder as the AppleScript itself. I have tried numerous ways, including using a tell finderblock like this
tell application "Finder"
set myFolder to container of (path to me) as text
end tell
set template_path to myFolder & "CC.psd"
tell application "Adobe Photoshop"
open template_path
end tell
What sort of magic do I need to perform?
PS: Currently this is running on my local drive, however it would be awesome if it would work on network volumes as well.
Thank you
Tobias Timpe

Related

Export Keynote slides to images with AppleScript

I have Keynote slides and I want automatically export them to images for further uploading to server using AppleScript. I've tried to manage it:
tell application "Keynote"
set doc to open "full-path-to-.key"
save doc -- this line didn't help
export doc to file "full-path-to-folder" as slide images
end tell
This script terminates with
error "Keynote got an error: The document “...” could not be exported as “full-path-to-folder”. Your most recent changes might be lost." number 6
Why?
I had a closer look at this exporting documents samples and ran into a problem with referencing a folder I want export my slides to. Then I've found that thing about it with a sample
set thePath to alias "Macintosh HD:Users:yourUserName:Desktop:"
So this one works properly:
tell application "Keynote"
set doc to open "/absolute/path/to/slides.key"
export doc to alias "Macintosh HD:Users:myUserName:further:path" as slide images with properties {image format:PNG}
end tell

Opening a Keynote Presentation using AppleScript (Keynote 6.5.2)

I am trying to see if i can change the text of some slides inside a keynote presentation that has already been created but i am having problems just opening it to begin with.
here is my current simple applescript:
set thisFile to "Users/myUserName/Desktop/KeynoteAppleScript.key"
tell application "Keynote"
activate
open thisFile
show slide 1
end tell
I get an error stating that the file I am trying to open :
"Users/myUserName/Desktop/KeynoteAppleScript.key" couldnt be opened
because there is no such file".
Thats obviously not correct, it is there and the i have double checked the name of the file to verify.
I am using Keynote 6.5.2
Script Editor Version 2.7, AppleScript 2.4
What am I doing wrong here?
Try adding a leading fwd slash to the path
e.g,
set thisFile to "/Users/myUserName/Desktop/KeynoteAppleScript.key"
I needed to do this to get it opening on 10.8, Keynote 5.3
ALSO..
When in doubt with a file path, try dragging the file from the desktop to the script window and it will insert a correct path.

AppleScript: open frontmost file with another application

I'd like to write an AppleScript program to do the following (Automator would be fine too):
I want to open the current active TextMate file (possibly there are several tabs open and other windows) with the application Transmit 2. (This will upload the file over FTP using Transmit's DockSend feature.)
Here I've used a specific application (TextMate) but ideally I'd like it to work for any file currently active in any application.
Ultimately I will assign a keyboard shortcut to run it.
Here's what I have so far:
tell application (path to frontmost application as text)
set p to path of document 1
end tell
tell application "Finder"
open POSIX file p using "Transmit 2"
end tell
I've tried many variants of this and nothing works.
EDIT:
I have found this page: http://wiki.macromates.com/Main/Howtos and someone has made exactly the script I'm looking for:
tell application "Transmit" to open POSIX file "$TM_FILEPATH"
This is for Transmit [not 2] and I think for TextMate pre v2. I get the error (when using Transmit 2):
Transmit 2 got an error: AppleEvent handler failed.
One of the updates to v2 has broken it (not sure which one).
There appear to be two steps to your problem. One, get the path to the document (or some other reference that allows you to later open the document), and, two, open the document in the desired application.
If the AppleScript is saved as an application, the frontmost application is the AppleScript you’re running, and so that path will be the path to the AppleScript application. In that case, I’m not aware of how to get the second-frontmost application.
However, if the application supports the scripts folder (go into AppleScript Editor‘s preferences, and enable “Show Script menu in menu bar”), you can save the script as a “Script“ in the User Scripts folder, and when run from the scripts menu the frontmost application will be the application you’re currently in.
You may want to display the p variable when testing to ensure that you are getting the correct path and not the path to the AppleScript.
As far as opening the document in another application (such as Transmit), the best way to do this is to talk to the application directly if it supports it:
tell application (path to frontmost application as text)
set p to path of document 1
end tell
--for testing: verify that the path is for the correct document
display dialog p
tell application "Transmit 2"
open p
end tell
I don’t have Transmit, but I’ve verified that this works if I replace “Transmit 2” with Textastic or Smultron 6.
If you do need to use the Finder to open the document, the Finder seems to prefer its paths as strings, and also seems to prefer a full path to the application. Something like this should work:
tell application (path to frontmost application as text)
set p to path of document 1
end tell
--for testing: verify that the path is for the correct document
--display dialog p
set transmitPath to path to application "Transmit 2"
set p to POSIX file p as string
tell application "Finder"
open file p using transmitPath
end tell
Again, I’ve tested this using Textastic and Smultron as the applications.
The most common solution for the problem you are trying to solve is to run an app that makes your Web server appear to be a mounted Mac disk. Transmit 4 has that feature, which Panic calls “Transmit Disk.” But there are a few other apps also — Transmit was not the first.
Your Mac apps (and AppleScripts) just see a typical Mac disk that they can save files to and read files from (the most basic of basic AppleScript tasks) and yet Transmit Disk (or similar app) is transparently mirroring any changes to that Mac disk to your Web server in the background. This makes all the network and FTP stuff totally go away and leaves you writing very simple scripts that do very powerful things to your Web server. You Save HTML documents on there, you Export image and movie files onto there as easily as you would Save them on your Desktop, and yet they are immediately published to your Web server. Even the only barely scriptable apps can Save their files onto a particular disk.
For example, if I have an HTML document open in BBEdit and I want to save a copy of that document to my Web server, it only takes a few lines of code, which would likely be similar in any AppleScript-able text editor (this script would also work verbatim in the free TextWrangler):
tell application "BBEdit"
set theHTMLSource to the contents of text window 1
make new document with properties {text:theHTMLSource}
save document 1 to file "Transmit Disk:index.html"
close document 1 saving no
end tell
Notice that the AppleScript above not only doesn’t have to know anything about SFTP or have any login credentials, it doesn’t even have to figure out the file path of my current document because it just takes the content right out of the current window. There are no POSIX pathnames, no shell scripts, no monkey business at all. And because this task and code is so simple, you could conceivably rewrite this script 20 times for 20 different apps that you may use, so that they can all Save a copy of their current document onto your Transmit Disk, and thus publish that document to your Web server.
And if I have a folder of images that goes along with that HTML document, I can ask Finder to duplicate that folder onto my Transmit Disk to publish it. With just one line of code:
tell application "Finder"
duplicate folder "images" of (the path to the desktop folder as alias) to the disk "Transmit Disk" replacing no
end tell
… but those images could also be exported out of Photoshop or any app, right onto the Transmit Disk, via AppleScript.
In short, the thing that all of your Mac apps have in common is they can all Save files to a Mac disk. They can’t necessarily all give you the pathnames to the documents they have open, or open those files with Transmit. And Mac apps and AppleScript were designed primarily to work with files Saved or Opened to/from local disks. So you gain a lot if you use something like Transmit Disk to make your Web server basically part of the AppleScript party, by making it appear to be just a plain old Mac disk.

Send all files on Desktop to Evernote then delete

Good morning,
I am trying to write an AppleScript that I can run that will send all the files on my desktop to Evernote, and then delete the files. My code to date is:
on run {input}
tell application "Finder"
select every file of desktop
end tell
tell application "Evernote"
repeat with SelectedFile in input
try
create note from file SelectedFile notebook "Auto Import"
end try
end repeat
end tell
tell application "Finder"
delete every file of desktop
end tell
end run
If I run this then the first and last 'tell' work fine (ie. the script highlights then deletes all the files on the desktop), but the middle 'tell' doesn't do anything.
However, if I manually highlight all the files on the desktop and then run just the middle 'tell' then it imports fine - each item into a separate note as intended.
As you can tell, I am new to AppleScript - I suspect I need to put the selected files in an array of some sort, but can't figure it out. Help!
Many thanks
Rich
Your code fails because there is no relation between your input variable and the selection of files via Finder – which means that your list is empty, and Evernote is not processing anything at all. You have obfuscated the problem by wrapping the Evernote import command in a try block without any error processing, which means all errors just go unnoticed (to avoid this kind of problem, it is good practice to always log the error message in an on error clause, if nothing else).
Also, you don’t actually need to select files on the Desktop via AppleScript to process them. The following code will grab all visible files (excluding pseudo-files like packages / apps):
tell application "System Events"
set desktopFiles to every disk item of (desktop folder of user domain) whose visible is true and class is file
end tell
Pass the list you retrieved that way to Evernote for processing:
repeat with aFile in desktopFiles as list
try
tell application "Evernote" to create note from file (aFile as alias) notebook "Auto Import"
tell application "System Events" to delete aFile
on error errorMessage
log errorMessage
end try
end repeat
and you are good to go.
Note that by judiciously placing the deletion command (right after the import command, inside the try block, inside the loop over all files), you make sure it is only called if Evernote does not error on import while avoiding having to iterate over the files several times.
A final note: you don’t have to use the block syntax for tell statements if there is only one command to execute – using tell <target> to <command> is easier and will keep you out of nested context hell.
Thanks #adayzone for corrections on list handling and alias coercion
Try
tell application "System Events" to set xxx to get every file of (desktop folder of user domain) whose visible is true
repeat with i from 1 to count of xxx
set SelectedFile to item i of xxx as alias
try
tell application "Evernote" to create note from file SelectedFile notebook "Auto Import"
tell application "Finder" to delete SelectedFile
end try
end repeat
Thanks #fanaugen

Applescript simple open save file snippet?

I just need a snippet to open and then save a file. It's an illustrator PDF with linked artwork, problem being the linked files do not update unless I open then save the file and I am having to do this sooooo many times it's making my brain explode.
Should be straightforward. I tested the following snippet in Illustrator CS3 (13.0.2) on Mac OS X 10.6.8.
set theFiles to choose file with prompt "Select the files" of type {"pdf"} with multiple selections allowed
tell application "Adobe Illustrator"
repeat with theFile in theFiles
open theFile without dialogs
save current document in theFile as pdf
end repeat
end tell
The choose file command (which prompts the user to locate one or more files) is just one way of obtaining a list of files. You could just as well use
tell app "Finder" to set theFiles to every document file of folder "Macintosh HD:Full:Path:To:Your:Folder" whose file type begins with "pdf"
if you want to get all PDF files inside a specific folder.

Resources