salmone Posted November 13, 2015 Share Posted November 13, 2015 Hi, I'm trying to create my first workflow; basically I assigned a shortcut key to open an app; ok that worked.. now I would like to add one step, after opening the app I would like the system to perform "Cmd+N", to create a new file in the app that I just opened. So basically it would be: I press a shortcut key and alfred would open the app and create a new file. Is this possible, or there is maybe a better way to do that instead? sorry for the really really newbie question, if you have any suggestion about that it would be great anyway Link to comment
vitor Posted November 14, 2015 Share Posted November 14, 2015 It is possible with AppleScript. In general, it is a very bad idea to script key presses, though, as it is imprecise and can lead to unforeseen results. The best option is to see in the app’s AppleScript dictionary if it has an option to create a new file (open Script Editor, do File → Open Dictionary…).However, if the only option is indeed emulating key presses (again, not recommended), you could do it also in AppleScript with something like tell application "System Events" to keystroke "n" using {command down}. Link to comment
salmone Posted November 23, 2015 Author Share Posted November 23, 2015 thanks for the suggestion Vitor, not having any scripting knowledge I managed to solve it doing like this: in automator I recorded the action that I was doing, -open the app and click on new file. Then in alfred I created a shortcut to play the automator actions. It works, though I'm sure that there are better ways to do that Link to comment
salmone Posted November 23, 2015 Author Share Posted November 23, 2015 (edited) I saw that the method I used is not working well, often it gives me an error. I tried looking in the script dictionary, but the app doesn't have a command do create a new document, the only things showing up are: window n : A window. elements contained by application. properties name (text, r/o) : The title of the window. id (integer, r/o) : The unique identifier of the window. index (integer) : The index of the window, ordered front to back. bounds (rectangle) : The bounding rectangle of the window. closeable (boolean, r/o) : Does the window have a close button? miniaturizable (boolean, r/o) : Does the window have a minimize button? miniaturized (boolean) : Is the window minimized right now? resizable (boolean, r/o) : Can the window be resized? visible (boolean) : Is the window visible right now? zoomable (boolean, r/o) : Does the window have a zoom button? zoomed (boolean) : Is the window zoomed right now? document (document, r/o) : The document whose contents are displayed in the window. responds to close, print, save. document n: A document. elements contained by application. properties name (text, r/o) : Its name. modified (boolean, r/o) : Has it been modified since the last save? file (file, r/o) : Its location on disk, if it has one. responds to close, print, save. I tried with this: tell application "iA Writer" activate make new documentend tell but it is not working.. then I tried with this: tell application "iA Writer" activate keystroke "n" using {command down} end tell but it isn't working.. if somebody has any suggestion.. Edited November 23, 2015 by salmone Link to comment
vitor Posted November 23, 2015 Share Posted November 23, 2015 You can’t tell an app to do the keyboard shortcut, you have to tell the system. Try tell application "iA Writer" to activate tell application "System Events" to keystroke "n" using {command down} make new document should’ve worked, though, that was a good try. Also, you may try asking their twitter account, which is very active. Link to comment
salmone Posted November 23, 2015 Author Share Posted November 23, 2015 (edited) yup! thanks Vitor, I was able to achieve it, I did like this: tell application "iA Writer" activate end tell delay 0.2 tell application "System Events" to keystroke "n" using {command down} I added the delay because I saw that without it often it was performing the cmd N when the active application wasn't IA writer yet, it seems to work really well, I'll test if everything keeps working fine in these days Edited November 23, 2015 by salmone Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now