Apple Automator take screen capture and save to user-specified location - applescript

So, I'm trying to use Automator under Mac OS Yosemite to create a service to allow a user to take a screenshot and save it to a location they specify, through some sort of "Save As" dialog. It seemed like it should be easy, but for some reason I'm running into difficulty with it. The screenshot component is easy, using the "Take Screenshot" action in Automator, but it's the saving it to a custom location that's causing me problems.
After trying a few different approaches, it seemed the easiest thing to do was to save the screenshot to a fixed directory/filename from within the "Take Screenshot" action, and then (using AppleScript) rename it in that directory, and move it to the user-specified target directory. So, I added a "Run AppleScript" action to my service. In it, I generate the dialog to choose a file name/path, using the choose file name command in AppleScript. I'm trying to split up the file name from the path, so that I can rename the file I save in "Take Screenshot," and then move it to the path that I'd like to save it at. I can get the full path, but am having problems just getting the filename from the path—and I've tried a variety of suggestions from what I've seen online. In my screenshot, the error shown was from attempting to do
I'm not set by any means on this flow, so if anyone has any better suggestions on how to do what I'm trying to do, by all means please let me know. Otherwise, if someone's able to just tell me how I can extract the filename from the path (and also if there's some special way you have to use that string to rename the file) that'd be great!
AppleScript code pictured in screenshot:
on run {parameters}
set thePath to (choose file name with prompt "Where would you like to save your file?")
tell application "Finder"
display dialog thePath as string
end tell
set UnixPath to POSIX path of (thePath as text)
display dialog UnixPath
end run
I tried this but it didn't work:
set basePath to POSIX path of (parent of (thePath) as string)
Thanks for checking it out!

An easy way would be to use the command line tool "screencapture". It has many options you can choose. See its man page. Here's an example that you can run as an applescript directly or you could put this inside an applescript automator action if you want.
Good luck.
set thePath to (choose file name with prompt "Where would you like to save your file?")
do shell script "screencapture -mx -T1 " & quoted form of (POSIX path of thePath & ".png")

I've created a Folder action in Automator. Choose Desktop. Add Find Finder objects (search: Desktop). Add Move Finder Object and choose your preferred destination. This will automatically move all your screenshots.

Related

exploring avaliable commands in an applescript program

I have an application and I can do the following command on (I know this because I googled for it):
tell app "TextMate" to reload bundles
What I would really like is to be able to ask the "TextMate" program:
tell app "TextMate" to list all commands
and have it list out all the things I can ask it to do:
... 'reload bundles', 'exit', 'open files'...
is there a way to do that with applescript?
The way to find all the commands of an app is to open its dictionary in your script editor. Usually "Open Dictionary ... " in the File menu, or drop the application onto the script editor.
[EDIT]
For applications that have AppleScript support, you can actually script opening the app itself with the Script Editor, a la:
set pathToApp to (choose file of type "APPL")
tell application "Script Editor"
open pathToApp
end tell
BUT this will be problematic with a non-scriptable app. You'll get an error, but Script Editor will actually open some part of the app (and it will be slow about it), then give you an unusable document. There's no way to catch this error. If you use the Smile script editor, you can use this method ...
set p to (choose file of type "APPL")
try
OpenDictionary(alias (p as string))
on error e
end try
... to open the dictionary of an app, and if it doesn't work (if the app doesn't have a dictionary), it returns an error but doesn't do anything else (but again, you can't catch the error and not have it complain, without hacking Smile)
[EDIT 2]
A rabbit hole to go down is trying System Events or the Finder to check for boolean of has scripting terminology property of a process, but I don't recommend it because I haven't found it to be reliable.
[EDIT 3]
Ach! I knew there was another method, but forgot what it was. As #mklement0 points out (thank you), you can do this to check for an app's script-ability prior to opening the app in Script Editor:
set pathToApp to (choose file of type "APPL") as text
set isScriptable to false
try
class of application pathToApp
-- only AppleScriptable applications have a class property
set isScriptable to true
end try
isScriptable

Mac Automator App: How to Set Folder

This is probably something simple but its driving me crazy...
I was just able to build my first automator workflow, which is quite basic and uses the Subtitles application to download subtitles for movies in a specific folder.
The Actions are:
Get Folder Contents
Filter Finder Items (to only movie files)
Open finder item with the Subtitle app
It works fine when I execute the workflow from within Automator because it has a "Folder Action receives files and folders added to:" clause in the beginning which tells which folder to use.
However, I want to save that as an application so I can schedule that on iCal but, when I save as an app, the clause which tells the folder is no longer available and looks like I need to pass the folder name as a parameter/argument and I have no idea on how to do that.
So, what I need guidance is on how to have the app executed in iCal while telling it which folder to use.
Thanks in advance...
Just add a "run applescript" action above your "Get Folder Contents" action. Make the applescript code look like the following. Make sure to put your folder path between the quotes.
on run {input, parameters}
return "/Users/username/Desktop/"
end run
Note: a quick way to get a path is to run this in Script Editor and use the result.
return POSIX path of (choose folder)

Creating an automator service to create a new document in the current directory

so I'm trying to create a service that will be located in the contextual menu of the Finder and that would allow to create a new document in the current directory.
I've been doing that using Automator:
Sorry everything's in French ^^
Anyway here's the AppleScript that I'm using to retrieve the current working directory:
on run {input, parameters}
tell application "Finder"
set pwdAlias to insertion location as alias
if not (exists folder pwdAlias) then
set pwdAlias to (container of pwdAlias) as alias
end if
end tell
set pwd to POSIX path of pwdAlias
return pwd
end run
Then I'm setting this value to a variable, then creating a new text document using the variable as the path for the document and finally I'm using the command Reveal in Finder to show the created document.
Everything's is working fine except that the script seems to always be late!
What I mean is that when I open a new Finder window and select my service, it is systematically creating the document on the previous window as shown below:
But then if I try a second time, the document is being created properly at the expected location:
And this is very systematic it happens every time!!
Sorry if I'm not very clear, it is not so easy to explain!
Well otherwise, I'm running Mountain Lion and here's the Automator project attached: create_new_document
To add the service just unzip and put the file under ~/Library/Services/
Hope to get some answers but I fear that this is just an Automator bug!
Try this
Depending on what you want to be clicking.
Set the Services selected to: 'folders'
or files or folders. in 'Finder.app'
Get first Finder Window path Action
You can download the Get first Finder Window path Action from my blog post here
The download is at the bottom of the post.
The Action gets the posix path of the frontmost finder window.
Since you are clicking on a folder in a window. that window will be the one returned.
Set Value of Variable
Get Specified Text
The next action 'New Text File' needs some input. If it does not get any, no file will be created. You can leave the text field blank. Just having the action in place works.
New Text File
Drag the Variable 'path' or what ever you named it on to the Where: drop down menu.
you can click the double blue lines at the bottom of the Automator window to toggle the workflow Variable List
Save your service. And try it.
(It may take a short while to show up in the contextual Menu.)
It's an open bug in 10.7 and 10.8
Use this Workaround
on run {input, parameters}
activate application "System Events"
activate application "Finder"
tell application "Finder"
set pwdAlias to insertion location as alias
set pwdAlias to (container of pwdAlias) as alias
end tell
return POSIX path of pwdAlias
end run

OSX Associate a shell script with a file extension?

I want to associate the .exe file extension with a shell script that launches wine. What is the best way to do this?
From what I've gathered, I need to create an AppleScript that will call wine, but how do I get the name of the input file in the AppleScript? If there is a better way to do this, let me know, but as far as I know this is the best way.
You can also use Automator to wrap your own bash, python or ruby script in an "Application".
Open Automator and choose to create an Application.
Find the Action "Run Shell Script" and double-click it or drag it to the script area.
Select the interpreter you want (bash, other shells, python or ruby).
Set the "Pass input" option to "as arguments". (In the automator model, the application "receives files and folders as input"; hence this lets your script see filenames as commandline arguments).
Enter your shell script in the edit area. On bash, use "$#" for the list of commandline arguments (individually quoted to protect embedded spaces).
You can now save the script (it will get a .app extension), and move it to the Applications folder or other reasonable location. It can be associated with a file type, like any other application.
NOTE: This works on Mountain Lion (10.8); can someone comment on how long Automator has been able to do this?
You can use an AppleScript application to do this - files are passed to the open handler, the same as a droplet, for example:
on open theFiles
set arguments to ""
repeat with anItem in theFiles
set arguments to arguments & space & (quoted form of POSIX path of anItem)
end repeat
do shell script "/path/to/wine" & arguments
end open
To associate a particular document type with your application, you will first need to add information to your application's information property list (Info.plist) to tell Launch Services what types of documents it can handle. See Apple's Document-Based Applications Overview (or take a look at the settings in other applications).

AppleScript Transmit Script for uploading file to replace itself on web server

So I have this idea for a handy little AppleScript which in my opinion would be very handy in speeding up the process of uploading a local file, to its same location on the server.
In other words, you have to specify the home folder on the server and locally, but once that's finished, it would be nice to just press like "Command" + "Shift" "U" for upload or some other hot key combination not in use by OS X for uploading the currently selected file in the Finder.
I find myself needing to do this a lot, and it will save a lot of time!
Someone please tell me if there is an easier way to do this, but I think this will be a good learning experience on top of it all.
I need some help on how I should get started however..
1) the command line program curl can upload files. 2) if you have a file selected in a Finder window applescript can get the selection. Using those 2 ideas you can automate your task. I don't know the exact curl command but that should be easy to find using google. So you select a file in the Finder and then run the script. The script can be run with a keyboard shortcut as you mentioned or just put it in the Script menu and run it from there.
tell application "Finder"
set selectedFile to item 1 of (get selection)
set selectedFile to selectedFile as text
end tell
do shell script "curl -switchesToUpload " & quoted form of POSIX path of selectedFile
I use Cyberduck which is an ftp client you can set it up so when you double click a file on the server it opens it up in your favorite editor.( textmate is my favorite.) it atuomagiclly downloads and uploads when you save.
this seems like a much better solution to the problem

Resources