Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I have the following code I did for a tweetbot keyboard shortcut to open tweetbot and create a new tweet.

on run {input, parameters}
    activate application "Tweetbot 2"
        tell application "System Events" to keystroke "n" using command down
    return input
end run

However, it always opens a new window for anything that is run in the background as well. This only happens when Tweetbot is not already running.

How can I fix this ?

share|improve this question
    
It opens a new window of what? AppleScript Editor, Tweetbot? –  patrix Nov 4 '12 at 20:01
    
Whatever is already running .. Opens a new applescript editor if automator is being used, opens a new safari window if safari is being used etc –  Render Nov 4 '12 at 20:05
add comment

1 Answer

up vote -1 down vote accepted

This is because the application is not in the foreground.

The activate command will not wait the response of the application when the application is not scriptable. So the script executes immediately the keystroke command, therefore the keystroke will be intercepted by another application.

activate application "Tweetbot 2"
tell application "System Events"
  tell process "Tweetbot 2"
      repeat until exists
          delay 0.4
      end repeat
      set frontmost to true
      delay 0.2
      keystroke "n" using command down
  end tell
end tell
share|improve this answer
    
Thank you @jackjr300 , but the code just runs infinitely and does not stop ... it puts tweetbot as the foreground but doesnt really prompt a new tweet ! Can you please explain the commands you have used ?? Im trying to learn how to script using automator –  Render Nov 5 '12 at 0:39
    
This is because the process name is not the same as the application. I don't know any Mac OS X application named "Tweetbot 2". The name is "Tweetbot" on Mac App Store. Check the name of your process with this script: tell application "System Events" to name of processes, execute it in the editor, look at the result. ---explanation of the commands: wait until the process exists, put the process to foreground, execute the keystroke. –  jackjr300 Nov 6 '12 at 3:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.