How to use Automator to bring window to front? - macos

I'm using a Mac, OSX 10.6 and I have a function in a desktop application that I want to automate. Manually I press Command+R wait for the application to read some data form a physical device for 1 minute, then press command+R again to take another reading (at this point it asks me if I want to save the data, so I press tab, tab and then space bar to select to save the data. I do this 3 times in total, so I want to automate the 3 times, so I can walk away from the computer and it will read 3 times automatically.
Is automator the best way to do this?
I've tried to do this already in automator by using the 'watch me do' function. This starts with the 'bring window Untitled to the front', and then the second command is press command+R. I then found a little piece of apple script to wait 1 minute and I plug the first action into that for the wait function.
However, when I click run or step, instead of going and opening up the correct window ("Untitled"), the cursor moves to the Media button in automator, and clicks that instead! But the application is definitely listed as the correct one.
Any help appreciated, but maybe automator is the wrong way to go?

Apple Script is the best way to go for things that don't require any "special processing" that would need to be done by a chain of different applications.
1) uging the AppleScript Utility
make sure that you have GUI scripting enabled in the "AppleScript Utility"
2) using the Script Editor choose File>Open Library and see if your application has any scriptable functions ... these may be a better way to go.
3) Create a new script and put in something like this ...
tell application "Firefox"
activate
delay 1 -- give it time to react
repeat 3 times
-- this gives us the keyboard
tell application "System Events"
keystroke "r" using {command down}
end tell
delay 6
end repeat
end tell
I used Firefox to test it .... should work for you ....
Once you've got the script you can use the save as to make it into an app or save it as a script in your ~/Library/Scripts folder or paste it into an automator workflow and schedule it with iCal.

I don't think automator is the way to go. You could use applescript, but you should take a look at sikuli. You will need to write the Sikuli script yourself, but what you describe shouldn't be difficult

Related

Custom Keyboard Shortcut Service Not Working 10.11 (el capitan)

I've seen a few questions dealing with this issue but nothing recently. I'm not sure if El Capitan (10.11) is breaking something, and I'm an AppleScript (and Automator) newbie.
I just want to have the basic functionality where a keyboard shortcut opens a new terminal window in the space I'm in. After doing the following things, absolutely nothing happens when I trigger the keyboard shortcut
1. Create an Automator Service
Open Automator and choose Service for my document type.
Set Service recieves selected to no input
Drag a Run AppleScriptaction into my workflow.
on run {input, parameters}
tell application "Terminal"
do script ""
activate
end tell
return input
end run
(At this point, running the workflow from automator by pressing Play opens a new terminal window correctly).
Save the service as Launch New Terminal Window
2. Assign a Keyboard Shortcut
Open System Preferences -> Keyboard -> Shortcuts -> Services
Assign Shift Command T to Launch New Terminal Window
When I try my new command, nothing happens.
Note, I tried to delete the service and retrace my steps. When I recreated a new service with the same name, the keyboard shortcut was remembered by the system. Does anyone know which .plist/where these keyboard shortcuts are saved?
If anyone can help me out, that would be great. A 30 second project has turned into 30 minutes of hair pulling.
I also encountered a similar problem. The issue is that, most probably some other application is using the key combination that you tried. You can check by using a bizarre key combination and check whether it works and then modify it. Try using something like Shift + alt + command + G. Or something like this ( use at least 4 keys ). It worked for me. Hope it helps you.
For future reference, since it seems a very common question, I think the only reason why a shortcut won't work is that it's being used by other application, as 2XSamurai said.
You can have quick feedback if the shortcut is going to work or not by navigating to Finder and then Services.
If your brand new shortcut is showing next to the service you created, that means is going to work, otherwise, it won't.
No need to perform other actions, you just need to pick a good shortcut. Somewhere on the web, you may also read that you must include the command key to make your shortcut work. That's also not necessary (see the screenshot with just ctrl + T combination working just fine).
PS I can't post pictures on answers yet so StackOverflow generated links to the images instead, but this caused the answer to broke (I was getting a message error stating there was code in my answer (??), so no screenshots, sorry, I hope the answer is clear enough even without them :)
I did exactly what you've described (Automator Service, no entry, only one Applescript Action, and assign the Automator Service to short cut key (in my case command shift Y).
All is working OK on my ElCapitain with bellow script in Automator action : (I mean the short cut opens a new Terminal window as expected)
tell application "Terminal" to activate
tell application "System Events"
tell process "Terminal"
keystroke "n" using {command down}
end tell
end tell
But I still ask myself the basic question : what do you really want to achieve ? usually the bash instruction could be done in do shell script command...no need for Terminal window... may be you have an other constraint.
I had the same problem before, solved it somehow, forgot about it and ran into it again when setting up a new MacBook today.
This solution works for me on several Macs:
Instead of using System Preferences -> Keyboard -> Shortcuts -> Services (screenshot 1 - for some reason I'm not allowed to insert pictures directly into posts) you can use
System Preferences -> Keyboard -> Shortcuts -> App Shortcuts (screenshot 2).
There you have to spell out the name of your service.
There are some issues with the App Shortcuts menu as well. For example a shortcut for the "Tags..." menu in Finder simply won't work but for services it works for me.

Final Cut Pro X with Apple script (export projects)

Trying to make a script with whom will launch FCPX and will export one project after another (per day need to export 24 videos). So far were able to make this script:
tell application "Finder"
open ("/Volumes/UpNext/Final Cut Projects/1-Monday/CurrentVersion.fcpproject" as POSIX file)
end tell
delay 14
tell application "System Events"
tell process "Final Cut Pro"
click menu item "Snapping" of menu "View" of menu bar 1
click
end tell
end tell
Instead of turning on snapping I want to do following: Click in FCPX on /File/Share/Some format.
Problem is that don't know how to launch "share" and "needed format", because "Share" is an pop up menu. My apologies for simple questions, unfortunately never in my life worked with scripts and project needs to be done ASAP. Maybe there is other way with scripts to achieve this goal (export many projects). Will be open to suggestions!
I think the best (maybe easiest) way to do this (at least to get you started) is to set up key commands in System Preferences, and to use the following method to trigger the key command.
So for example, I just used System Prefs (Keyboard>Keyboard Shortcuts>Application Shortcuts) to make a Shift-F1 key command to trigger the "Vimeo..." menu item. Now I can use cliclick to trigger that. Cliclick (http://www.bluem.net/en/mac/cliclick/) is somewhat limited (well, more limited than the version I have on my machine :-) ), but it can come in handy. You just have to use an AppleScript do shell script command to use it:
activate application "Final Cut Pro Trial"
delay 0.5
do shell script "/your/path/to/cliclick kd:shift kp:f1 ku:shift"
The delay makes sure everything settles before the click (w/o the delay, click may fail).
(Obviously, replace the path in the example with a correct POSIX path -- mine is installed in /usr/local/bin/)

Increase speed of applescript keystroke

I'm using applescript to automate some browser activity. I have it tied to the speech recognition, so that I can make some custom voice commands.
One such command is "I wanna go home", which when heard, pulls up the relevant bus schedule on my browser. Given that the public transit system uses PHP and GET, the URL gets pretty long. Therefore, the keystroke myURL in the applescript takes a while to execute (something like 1-2 seconds). While I can live with losing 1-2 seconds, I'd really rather not.
With that in mind, is it possible to send these keystrokes faster? I believe I read somewhere that using key code is faster than using keystroke, but for the life of me, I can't figure out where I read that.
EDIT: My browser of choice is Google Chrome, which I couldn't find URL hooks for. This is why I had to resort to using keystrokes. Therefore, I'd prefer answers that work with Chrome over Safari
Another way to solve this is to put the text in the clipboard, then paste it. You can save the clipboard contents first and put it back afterward, so you don't lose what's already there. This method works in other situations when you want to enter a lot of text, not just for browser URLs like the other answers.
set clipboard_contents to (the clipboard)
set the clipboard to "Some long string that would take a while with the keystroke method"
tell application "System Events" to keystroke "v" using command down
delay 0.2 -- needed because otherwise the next command can run before the paste occurs
set the clipboard to clipboard_contents
I'm pretty sure you can script your browser to open the URL directly, instead of typing in the keystrokes (faking input events in AppleScript should be considered a last resort).
You can do this with the open location standard addition:
open location "http://google.com"
Open location is defined in Standard Additions and should not be enclosed in a tell statement.
open location "http://google.com"
If you want to target a specific browser you can use:
tell application "Safari"
if not (exists document 1) then reopen
set URL of document 1 to "http://google.com"
end tell
To target Chrome:
tell application "Google Chrome"
if not (exists window 1) then reopen
set URL of active tab of window 1 to "https://www.google.com/"
end tell

Xcode, default window layouts?

No-one has been able to answer this question.
It would seem to be impossible to have XCode open this way.
However Youssef provided the most useful answer - so Youssef gets the points, thank you Youssef.
Note for future readers .. the mac utility Moom is excellent for some, but not all, problems of this nature. Again it is not a total solution.
Using the current up-to-date XCode,
Whenever I OPEN a project in XCode (or start a new project),
it looks like this:
However, I want it to look like this:
Again that's when I OPEN a project.
Is there any way to achieve this? Thanks.
Go to Xcode (in the top menu bar) --> Preferences...
Switch to the Behaviors tab and modify what you want in the Running section
You'll get something like this:
You can choose to show/hide what you need, and specify the default behavior
Cheers
EDIT:
After understanding what you need, I will modify my answer.
There is no way to force XCode to automatically enter a behavior at startup.
The best you can do is to create a custom behavior and give it a keyboard shortcut. And when you start XCode just use the keyboard shortcut to enter the desired behavior.
This is how it is done:
In the behaviors tab (above) click on the + sign at the bottom
Then enter the desired name of this behavior (Maybe StartupBehavior)
Click on the left button to modify the shortcut key. My preferred shortcut key is Command + ` (the button to the left of the '1' key). This is because it will not override any other command and it is easy to click.
Note: you can specify any shortcut you like and dont be afraid to override one that already exists if you don't use it.
Now you need to configure your behavior the way you like it
Close the preference windows.
Now everytime you start XCode just use the shortcut key you specified (Command + ` in my case) to quickly load your behavior
Note:
If you dont like keyboard shortcuts you can also load your behavior by going to Xcode (menu bar) --> Behaviors --> Select your behavior
That is the quickest way you can achieve what you want. I dont think you will be able to force Xcode to run your behavior automatically on startup.
Hope it helped.
Cheers
(not really default layout, you have to hit at least 2 keys simultaneously once XCode is open) You could set up a behavior for "Build->Starts" and hit "CMD + B"
after opening XCode / any Project. This way it will 1. change to your
Layout even if there are any errors and Build fails and 2. you don't
have to Stop it again.
As already said, it's not a default layout, but I like to let the compiler run through projects of other people at the beginning anyway.
You could search for some Keys in XCodes preference file in
~/Library/Preferences/com.apple.Xcode.plist as already suggested in
my comment, I'm not experienced enough to know which are the right
ones, sorry.
Wrap the .xcodeproj File opening in an application. E.g. in no way "clean", but anyway:
Set up an XCode behavior like in option 1
Open Automator, choose Application, drag "open Finder Items" and "run AppleScript" inside.
on run {input, parameters}
set frontmostAppName to name of (info for (path to frontmost application))
repeat while frontmostAppName is not "XCode.app"
set frontmostAppName to name of (info for (path to frontmost application))
delay 0.5
end repeat
try
tell application "System Events" to keystroke "b" using {command down} --simulates CMD + B
end try
return input
end run
Insert some applescript like the above and save the Automator application.
RightClick -> getInfo on any .xcodeproj File in Finder/on Desktop, in "open with" select your new Automator app, click add and back in the info panel -> check "Change All"
You can obviously create a new Behavior instead of using Build->starts, assign a different Hotkey in XCode and script e.g. tell application "System Events" to keystroke "i" using {command down, control down, shift down, alt down} , optimize the applescript somehow or do everything via a real cocoa app if you want
Well, you may be able to approach it using preconfigured xcuserstate files nested in your project templates' default workspace directory, but that is going to be a pain to tweak and maintain. I'm not even sure it would work.
I would personally just approach it by using Behaviors.
So:
1) create a Behavior
2) Give it a convenient key binding
3) Then define the Behavior. For example:
ASCII Behavior Editor:
...
√ Show *Debugger* with *Console View*
√ *Hide* utilities
...
That should do it. Just hit the assigned key command when you want to restore that display state, regardless of its initial state.
Of course, you can do a few other things or all sorts of arbitrary stuff by running an external AppleScript (triggered with a Behavior's run script facility).

Google SketchUp close file

The Ruby API to Google SketchUp has a function, open_file, but I can't find a close_file function. Since I have to batch process many files, I want to close each file before moving on to the next, otherwise the program will crash from memory exhaustion.
What is the best way to close SketchUp files programmatically?
I am using Mac OS X and am willing to use AppleScript functions to signal the window to close.
EDIT
I am considering a few approaches that have proven fruitless so far.
Using the appscript Ruby gem, as described in this question. The problem here is that I cannot get SketchUp to recognize my installed gems.
In a similar vein, I am trying to use osascript (a bash program that executes AppleScripts from the shell) to close the window. That is, I call out to the shell from SketchUp's Ruby console window using one of the following:
%x[osascript -e 'tell application "SketchUp" to close window 1']
%x[osascript -e 'tell application "SketchUp" to close window 1' &]
%x[osascript -e 'tell application "SketchUp" to close every window']
%x[osascript -e 'tell application "SketchUp" to close every window' &]
Whenever I try this second approach, SketchUp just freezes. However, when I execute any of these commands from an IRB or directly from the Bash prompt outside of SketchUp, I get the desired behavior: the model window closes (incidentally, the Ruby console window remains open, which is fine).
Have a master script that launches a slave script to process each model. The slave will run within the Google SketchUp program while the master waits. When the slave is finished, it signals the master, and the master closes the SketchUp file. To do this interprocess communication, I tried using drb. However, when I try to require drb within SketchUp, I get the following message:
Error: LoadError: (eval):5:in 'require': no such file to load -- drb
EDIT 2
Having a separate process continuously running that closes Google Sketchup windows using AppleScript when signaled is clumsy for a number of reasons. First, it's ugly to have to have a separate process devoted to closing Sketchup windows. Second, the only effective way of communicating with the external script is through the creation of files, which is wasteful and the disk access may be slowing things down.
However, the most severe issue is that Sketchup is slow at responding to AppleScript commands. I have a pretty computation intensive script running in Sketchup, and it seems to starve the AppleScript response, which means that the osascript times out before the windows close. Sketchup only gets around to responding to AppleScript when there is a dialogue box prompt in Sketchup that pauses the execution of my computationally intensive script.
EDIT 3
I have modified my close_file function to pause execution of the script by displaying a dialog box. This essentially yields the current thread and allows the thread that responds to AppleScript commands to execute:
def close_file()
f = '/temp/mutex.txt' # for finer control, use different mutex for each window you want closed
File.new(f, 'w').close
result = UI.messagebox "Click OK when window has closed."
end
Then the separate ruby script that closes windows via AppleScript will additionally have to click "OK" in the dialog box. AppleScript to do that is:
tell application "System Events"
tell process "SketchUp"
set frontmost to true
keystroke return
end tell
end tell
This modification is an improvement. It corrects the "most severe issue" mentioned in EDIT 2, but the other issues remain.
There is an important limitation with using an external AppleScript to force SketchUp to do something. SketchUp will not accept any user input while a menu script is running.
I found this out trying to setup an automated rendering solution. I added a menu item which opens a WebDialog to get a list of models to download from a server. It then steps through the list and uses cURL to download each model. Once downloaded it loads the model, renders it out, uploads the images using cURL again, and goes to the next model.
What I wanted was to activate the AppleScript after each model is rendered (using the "mutex" file solution shown above) and have it close the model window. Unfortunately, since the menu script is still running, SketchUp will not respond to anything the AppleScript tells it to do. Only once all of the models have been processed and the menu script exits, will the AppleScript finally run.
This would not be so bad if all of the calls to the AppleScript were queued up, but they aren't. It seems that only the last two get honored (that number may be just an accident dependent on the timing of when my calls to the AppleScript were made relative to when the menu script finished).
Because of this limitation, I was not able to use the mutex wait() function in my menu Ruby code. Since the AppleScript was not able to execute, it never created its "I'm done" mutex file, and therefore the wait() function in Ruby never got the signal that it could continue. The result was a deadlock between AppleScript and SketchUp, such that SketchUp just freezes up (well, actually it is just stuck in a really tight loop in the wait() function).
So, when approaching this problem, think of AppleScript as a cleanup tool you can call when all of your processing is done.
One solution is to have a separate process continuously running that will close Google Sketchup windows using AppleScript when signaled. The approach described here uses files to do the interprocess communication (drb did not seem to work).
First, you have to make sure Sketchup supports AppleScripting. To do this, execute the following at the command prompt (with appropriate substitutions for different Sketchup versions, etc.):
$ defaults write /Applications/Google\ SketchUp\ 8/SketchUp.app/Contents/Info NSAppleScriptEnabled -bool YES
Now, create a separate file that will close Sketchup windows. Here is my closer.rb file:
#!/usr/bin/ruby
## closer.rb ##
def wait(f)
while !File::exists?(f)
end
File.delete(f)
end
def signal(f)
File.new(f, 'w').close
end
while true
wait('~/temp/mutex.txt')
msg = %x[osascript -e 'tell application "SketchUp" to close every window']
signal('~/temp/conf.txt')
end
Run this script from the shell. It waits until the file ~/temp/mutex.txt is created. Once the file is created, it runs osascript (essentially AppleScript) to close all the windows in Sketchup, and then signals that the windows were closed by creating a ~/temp/conf.txt file.
Here is the client code (which can be placed in your Sketchup plugins) that signals the closer script:
def wait(f)
while !File::exists?(f)
end
File.delete(f)
end
def signal(f)
File.new(f, 'w').close
end
def close_file
signal('~/temp/mutex.txt')
wait('~/temp/conf.txt')
end
The close_file signals the closer script and then waits for a confirmation that the file was closed before returning. Now you can close files in Sketchup.
You can do a lot with SketchUp using applescript and/or Automator.
tell application "SketchUp"
activate
tell application "System Events"
delay 1.0 --close window, adjust delay to suit
--key code 13 using command down -- Press ⌘W or you can use
keystroke "w" using command down
end tell
end tell
I have a number of status bar items for open new, paste to console, closing, killing SU etc.
They're tiny files that trigger the SU shortcut keys, and can be used externally, from 'Ruby Console' or as part of a plugin.
I'm not sure what kind of 'batch convert' your trying to achieve, but
I used Automator to convert most of my old v5, V6, v7 files to v8 today...
100's of them,
and your probably aware, on opening you get 'alert' warning that saving will convert the file to the latest version (which is what I wanted).
It's tricky to click this "OK" programatically.
Automator on it's own is quite restricted, but you can add an applescript to the workflow and I've found that if you use it's 'record function' to demonstrate what you need to do.
You can then copy/paste that code into 'Script Editor', get rid of the 'dowithTimeout' bits, copy it back into the workflow with delays either side and let it run.
I had to play around with delays, to accommodate some larger files, but achieved >95% success. [Success being able to select all, right click, Create Icon and Preview.]
Unfortunately I then binned the workflow, but could recreate it, if you want to have a look.
success, in Ruby Console
system("osascript -e 'tell application \"suoff\"' -e 'activate' -e 'end tell'")
and this script inside an Automator.app in applications folder....
on run
-- Make sure SU is foremost, don't click on anything else either
tell application "System Events"
tell process "SketchUp"
set frontmost to true
-- gives a message if you try to select when SU's not running
delay 0.2
tell application "SketchUp" to quit saving no
-- no dialog box etc...
delay 0.1
end tell
end tell
end run
the problem is SU has an anti self destruct ruby, that mines any linked files and aborts all efforts to shut it down... from inside itself.
so you need to bury it...
this combination works on 10.5.8 with SU8.1.
if you have a look at SketchUcation [Developer] Forum, I left a call for mac/applescript testers... I've rewritten the app since then but if you PM me I'll send you an open copy of all the bits I've got working..
john
I had to convert hundreds of .skp files with a batch processing script. I faced similar issue, since every time I opened a new file it remained opened. For me it was enough to close the active_model and the file goes with it.
This was true for SketchUp 2019 Pro Mac OS X version.
model = Sketchup.active_model
model.save(filename, Sketchup::Model::VERSION_2015)
model.close
Hope it will help

Resources