Command-Z in Applescript for current active application - command

I'm trying to make an undo function on AppleScript for the current active application.
My code actually works on the automator itself when I run it, but it doesn't when I launch it as a service via shortcut.
on run {input, parameters}
tell application "System Events" to keystroke "z"
end run

You need to send the keystroke using {command down}
tell application "System Events" to keystroke "z" using {command down}
Also, I don't think a service will accomplish what you are trying to do. A background application which is called through spotlight would probably work better. However, the work of calling any solution seems to be as much if not more than simply typing command z manually.

Related

Shutdown a program which constanly changes window name with AutoHotKey?

I've recently started creating some simple scripts with autoHotKey, and I'd like to know if it is possible to close Spotify, which changes window title for every song.
I've tried with
Process, Close, Spotify
but it doesn't work unfortunately.
The following code snippet works for me:
while(Winexist("ahk_exe Spotify.exe"))
WinClose
Spotify is weird in that it appears to create many processes of itself when launched,
and that when you run a command like WinClose, ahk_exe Spotify.exe once, it does not immediately close the program (you can still use and interact with the GUI, but if you attempt to move the window by dragging the top bar, Spotify crashes).
However, if you spam that command over and over again, Spotify eventually closes as intended. So, in order to deal with this, let's create a program such that while there still remain Spotify processes, close Spotify Processes. Then, once there are no Spotify processes remaining, stop trying to close Spotify.

Add Operation to File Menu in an Applescript Application

I have an application that was made with applescript for now I can substitute this for the code:
try
tell application "System Events"
delay 10
display dialog "blah blah"
end tell
end try
and I would like the user to be able to quit this application by either rick clicking on it and selecting "Quit" or by going to File>Quit. But I believe the try is catching the quit. Is there any way to do this?
You can't do what you ask. Even if possible, the delay statement stops everything for 10 seconds so it can't catch any "quit" actions. If you explain a little more about what you're trying to do then there may be another way to accomplish your goal.
Note also that in Yosemite the delay command isn't working properly. It's a bug. You can use this instead until the bug is fixed.
do shell script "sleep 10"

Autohotkey sometimes stuck

Prehistory:
Some time ago Skype has assigned Ctrl+R hotkey to make a new call. This setting can't be disabled.
Sometimes I confuse browser window with Skype window and made a call to many people instead of refreshing browser window (I need to refresh browser window very often because of profession).
So I made a script to prevent sending Ctrl+R hotkey to Skype window, it works perfect, but sometimes I can't send Ctrl+R anywhere, not only Skype window.
Code:
;#NoTrayIcon
$^r::
WinGetClass, class, A
if (class != "TConversationForm")
Send ^r
return
Compiled binary: http://random.net.ua/SkypePreventHotkey.exe
Autohotkey version: 1.1.14.03
I have two improvements:
Use #If(...) for context-sensitive hotkeys whenever possible. In my experience, #If is more reliable due to various reasons. Also, you won't have to worry about non-affected windows (e.g. by deciding what to send); in fact, I believe that non-affected windows receive the original native keystrokes if the #If... condition comes up false.
AHK_L has a cool window selector called ahk_exe which lets you identify windows by their process name, rather than their class/title. It's possible that different windows of the same program have different class names, e.g. Windows Media Player in minimized/maximized mode. Possibly, Skype has that, too. That's why for more complex applications, it is often reasonable to identify by process name.
Mixing these suggestions into a solution, we get this brief piece of code:
; I hope it's skype.exe ;)
#IfWinActive ahk_exe skype.exe
^r::return
Instead of programmatically saying "If the active window isn't Skype, we send CTRL + R", we're now saying "If the window is Skype, we do nothing".
You could try something like
#InstallKeybdHook
SendMode, Input ; <---typically more reliable than send play mode
~$*^r:: ;<--- ~ means pass command to machine * means fire regardless of other hotkey modifiers.
Send, {Blind}{Ctrl Down}
Send, {R}
Send, {Ctrl Up}
return
I use a program called ACT! for my database. Whenever I script hotkeys it like to be sent commands like this vs. what you initially had (not that it was wrong).
If that doesn't work you could try adding: Sleep, 50 between each send (produces a delay) to see if that works. If so, you can cut each delay by 1/2 until it stops working again.
-islanq
Another solution using Autohotkey
#IfWinActive ahk_class tSkMainForm ;Skype
;prevent call
^r::
#IfWinActive

AutoHotkey Win+X Power Users Menu

Anyway to get AutoHotkey to pop the Power Users Menu in Windows 8.1? Typically accessed via Win+X. I can get AHK to perform other Windows key based key combinations, such as Win+D, but the Win+X refuses. I have several other AHK scripts that work fine, all UAC issues have been eliminated. I have tried 'Send, {LWin Down} x {LWin Up}', 'Send, #x', changing SendMode to all various modes had no effect. Running the AHK script as Admin had no effect, not surprisingly since I am running as Administrator and I have UAC turned completely off.
You may ask why I want to do this. The reason is I want access to the 'Shutdown or sign out' menu, so I can trigger either restart or shutdown. This is so I can use an AHK to trigger my machine to restart or shutdown (laptop). I am doing this, because what I had been using previous to Win 8.1 stopped working, which was was, for example to shut down, 'Shutdown, 9'. The computer shuts down, but then starts back up, totally strange. When I use the Win+X menu, or Start8 (highly recommended) it works fine, and stays off. I even tried skipping AHK and was via my MS keyboard able to trigger a command-line one liner of 'Shutdown -s -f -t 00' which behaves the same as the AHKs attempt at shutting my computer down. And to review, the Win+X and Start8 shutdown options work fine.
So far this is WAY more work than it should be to trigger a shutdown from a button on my keyboard!
Any help is much appreciated.
EDIT: Example code was requested, please find the AutoHotkey Script shown below, as described the Power Users Menu does not appear.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Play ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
MsgBox, 4097, , ---------- The system will restart in 5 seconds. ----------, 5
IfMsgBox Timeout
{
;Shutdown will continue
}
else IfMsgBox Cancel
{
MsgBox Shutdown canceled.
return
}
;Does not work
;Send {LWin Down} x {LWin Up}
;Does not work
Send, #x
In case anyone else is looking for a solution to this problem, you have to set focus to the Windows taskbar before sending the keys, like this:
WinActivate, ahk_class Shell_TrayWnd
Send, #x
Maybe there is some other program that uses win+X. If another program assigns it's hotkey after your script runs, it is possible that it can hijack the hotkey.
Also, I don't know about windows 8, but in XP you could assign a hotkey to a shortcut, and there isn't any way to figure out which shortcut it belongs to. I found this out the hard way when I installed a certaing program and that app's installer set a hotkey to the shortcut - I couldn't get it to go away. I finally tracked it down.
Could you have a similar issue?
Also, if you use this, does it work? It is a good test for you.
#x::
msgbox, Hey, Bob!
return
If that works, then it may be an issue with whether win+X can be (or is being) accepted by the active program.

Changing window focus on OS X

As part of an InstallationCheck script on OS X I need to use finder dialogs to let the user browse for files. When I'm done I want to move the installer application up front again so that the user can easily continue with the installation.
I have already tried the simple:
tell application "Installer" to activate
This does not work because as long as I am inside the script the Installer application is unresponsive and when i try to activate it the applescript will try to wait until Installer responds, effectively locking the program until the InstallationCheck script times out.
So basically I need a way to focus an application that works even if it is currently unresponsive. Is there any way to do this either from an applescript or directly from the perl script?
try
ignoring application responses
tell application "Installer" to activate
end ignoring
you could also try something like this
tell application "System Events"
set installer to application file of application processes whose name is "Installer"
end tell
tell application "Finder" to open installer