Use Automator as an Applescript learning tool? - macos

I am trying to teach myself AppleScript. Is Automator based on AppleScript? It occurs to me that if there is a way to view the AppleScript "guts" of an Automator action, that would be a great learning tool. But is that possible?
Thanks!

The best way to learn AppleScript is indeed to learn by example. MacScripter is the best place to start, without any doubt. Checkout the tutorials section and follow threads. Most problems you come across are already answered at MacScripter.
Automator is not based on AppleScript, it uses ScriptingBridge and mostly private API's. ScriptingBridge is an alternative to AppleScript for Cocoa developers (Objective-C), the syntax differs however greatly from AppleScript.
In conclusion, Automator is certainly not the place to start learning AppleScript.

Go to the link below and look at the "AppleScript Tutorial for Beginners" series. It's how I learned. Also you have a folder full of applescripts if you want to see a variety of code. Find it here: /Library/Scripts/. Also, under the help menu of AppleScript Editor is the "Applescript language guide". Finally, you will have to learn the terminology of the scriptable applications, so in AppleScript Editor file menu choose "Open Dictionary..." and choose an application in the list to see its applescript terms.
http://macscripter.net/viewtopic.php?id=25631

Related

Applescript to Swift Language (application control)

I wasn't sure what to call the title, but I'm working on an Applescript that pulls/pushes information from iTunes and save it. During the development of my Applescript I figured that if I'm going to learn a language, I'm going learn Swift as I want to develop more advanced programs down the road. Even though Applescript is as useful, and what I'm doing is working in Applescript... I wanted to take the next step in Learning to develop programs and scripts using Swift. I want to be able to replicate what I was able to do in Applescript using Swift.
What I wanted to know is if there is a tutorial/guide on how to read data from the accessibility inspector/etc and control programs like applescript is capable of doing, etc...
To give you a background on what I was able to do in Applescript, it's a really basic version of Batch Apple ID Creator. I work for a small school district that is now using a MDM to manage the i-devices. Well, with the MDM, each device needs to have it's own apple ID and manually creating 500+ apple ID is not as much fun as it sounds. These are some one the steps that I recreated in Applescript.
Check to see if iTunes is running
Check to see if there is a current user logged in.
Click "Sign Out" under menu bar if current user is logged in.
Goto "Create Apple ID..."
etc.
I took the current script that is up on that Git repo and made some changes to it so it works with the latest version of iTunes (12.1.2). After making some changes, I figures I wanted to learn and started rewriting the script to fully understand how it works and to learn Applescript syntax.
Let me know if you need more information about what I'm trying to do.
Throdne
I am investigating this sort of thing myself. What amazes me is that you can use Swift as a scripting language. I've written several short scripts that use the simple 'shebang' line #!/usr/bin/swift
To access AppleScript functionality you almost have to call AppleScript (through Cocoa) from your Swift code.
Here's an example I found elsewhere:
Swift Code
let myAppleScript = "..."
var error: NSDictionary?
if let scriptObject = NSAppleScript(source: myAppleScript) {
if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(
&error) {
println(output.stringValue)
} else if (error != nil) {
println("error: \(error)")
}
}
try searching for 'call AppleScript from Swift' (or Cocoa) or 'call AppleEvents from Swift' (or Cocoa).
Each language has a specific purpose. AppleScript's strength is interprocess scripting through AppleEvents, while Swift's strength is programming large full-featured applications. With AppleScript, you can get the contents of a webpage from Safari using JavaScript, parse it with shell script, and send it in an e-mail with Mail. Most languages can't do such a thing as easily as you can do it in AppleScript (Swift included). They are complementary. You can do amazing things by learning both, but they each have their own limitations and strengths.

Toggle enable/disable for Automator app

I've got a monstrously large Automator application designed to be used by others without advanced-level computer skills. Basically, at a couple of points, I'd like users to be prompted to enable or disable the action that follows. The closest answer I've seen is:
automator enable/disable/delete action in run applescript
This is promising - I'm guessing this applescript method is the way forward. But this works for Automator workflows, not applications. I know so little about Applescript - even if it is possible, I wouldn't know how to do it. Anybody have any ideas?
I suggest breaking up your huge workflow into smaller pieces.
One workflow can call other workflows, but workflows don't have a conditional statement. There is no way to do an if-statement, a conditional for a workflow.
Automator actions such as "Run Shell Script" and "Run AppleScript" both have conditionals in them (if-statements).
AppleScript allows the ability to call upon applications to get things done, and allows you to display simple dialogs.
Shell scripts allow you to go deep inside of OS X, harnessing the power of Unix to work for you.
Both AppleScript and shell scripting would be useful for you to add to your knowledge of Automator.
AppleScript was actually designed to work with Mac applications. Look at the application:
/Applications/Utilities/AppleScript Editor.app
Beginner's tutorial on AppleScript:
Beginner's tutorial on the Mac's Unix command-line:
-- Kaydell
kaydell#yahoo.com
http://learnbymac.com

Open an application in a space using applescripts

I am trying to create a script that will open an application in a specific "space". So let's say I am on space 1 working in the terminal and then I want to be able to open safari in space 4. Is there a way to do this?
I have done some searching and found only ways to set the system profile options. Maybe I should tell you my end goal in case what I am attempting is not possible.
I use a laptop and plugin in to multiple stations, home, office, and travel. I want to create different window layouts for each one. So I will need an apple script telling it to open applications in varios spaces and different dimensions. I hope this makes sense. Ask me for clarification if it doesn't thanks!
PS the answer doesn't necessarily have to be any applescript I just thought that would be the easiest way :)
Here's a list of applescript commands for Spaces. There's a couple things there that might help you.
It is possible to do some scripting of application Space preferences by using the scripting interface to the System Events.app. See the answer to a similar question here.
The easiest way I've found is via GUI scripting. Make sure the Spaces menu is active on the upper right of your computer. That lists the spaces by number. You can then just write a GUI script to select the menu item of the space you wish. That will switch to that space. Then do an activate Applescript to open the application there.
Let me know if you need sample code demonstrating this. I have some in Python + Appsscript that does this but I should be easily able to convert it back to Applescript proper if you need it.

AppleScript Editor record doesn't work

I have opened the AppleScript Editor and pressed Record button.
Then I run TextEdit, create a file and put some text there.
When I click the Stop button in AppleScript Editor, nothing was recorded, the window is blank.
What is the problem?
You can use the Record feature of the Automator to record the UI interaction steps needed to do the relevant workflow. Then you can then literally select and copy the recorded steps in automator and paste them into a new Applescript Editor window. This will give you applescript which may or may not work. You'll probably want/need to edit the resulting script, but at least it should help give an idea what is needed to achieve your workflow programatically. This method is usable regardless of whether or not the target application has an applescript dictionary or supports the AppleScript Editor Record button, as it is the interaction with the underlying UI elements which is recorded.
Steps:
Open Automator
Start a new "Workflow"
Start recording
Perform whatever steps you require with your app (in this case typing into textedit)
Stop recording
This will create a list of actions in Automator like:
Select all these and copy (CMD+c)
Open the Applescript Editor app
Paste (CMD+v). The result will be valid applescript to perform the actions you just recorded:
Note that as is generally the case with UI automation, the automator records steps exactly and the script plays them back exactly. This my not be exactly what you want - e.g. if a different application were active, the text could get typed in there instead. The generated applescript should be used as a guide to the final applescript.
The problem is that applications need to explicitly support AppleScript recording in order for it to work, but almost no applications actually do. Finder still supports it a bit, and maybe a couple other apps (BBEdit comes to mind), but for the most part, AppleScript recording has been pretty useless for quite some time.
Not all apps are recordable (in fact, only a small handful are). Recordablity is something each app needs to implement, and I guess TextEdit isn't recordable.

run applescript on 2x-click

OK, this feels like an idiot question, but I'm stuck - I don't know the first thing about AppleScript. I have a .scpt file and I want to double-click it and just have it run, but instead every time I click, it opens up the AppleScript Editor. This feels like it should just be an option on the file, but I'm missing something obvious.
Please help me feel less dumb, thank you.
From the “File” menu, choose “Export”; there’ll be a “File Format” dropdown underneath the file browser. To get a double-clickable application instead of a document, choose “Application”. This will produce a .app bundle like ordinary Mac applications (this will also let you package other resources with your script if you need to). You can choose “Run Only” or not; if you do, then anybody with just the .app won’t be able to edit your script further, since it’ll be compiled. (But if you’re saving a copy as the application, that might be what you want.)
Another option, as per an anonymous user on Ask Different, would be to save/export your file as a “Script” (.scpt) or “Script Bundle” (.scptd), save it in ~/Library/Scripts/, and check “Show Script menu in menu bar” in Script Editor’s preferences.
(If you’re running an old version of OS X, the first version of this answer has the information you’re looking for.)
There's more than one way to do it; i have found this to be the simplest:
In sum, you create an Automator application and place your applescript inside it (easier than it sounds, and it's not a hack either--there's actually a specific Automator action for this). Then when you are finished, you select "File" from the menubar, next "Save As Application", then select a location. Now check there and you'll see the newly-created Automator icon (little white robot holding a grenade launcher).
You can do anything that you would ordinarily do with this application icon--double click to open, drag it to your dock, etc.
Appstorm has created an excellent step-by-step tutorial for building an applescript-embedded automator action. On the page i linked to, the tutorial author has also supplied an Automator script that you can download and use as a template.
While it's certainly not the simplest route, one benefit to running your script from Automator, as doug suggested, is that you can set a hotkey or keyboard shortcut to execute your script if you hide it in an Automator Service (OSX 10.6+). See:
http://blog.fosketts.net/2010/08/09/assign-keyboard-shortcut-applescript-automator-service/
When you save a new script, a menu should appear asking what you want the file name to be, where it will be stored, any tags for it, and what script format you want it to be. There should be 4 scripts formats:
Script
Script Bundle
Application
Text
The script format you want to use would be "Application." This will turn it into a double-click application if its not in the dock.

Resources