I'm not sure what i am doing wrong but after this command finishes, the script ends yet there is still another command to complete, would anyone know what I am doing wrong. Thanks
I enter this in, it runs through
tell application "Finder"
activate
display dialog "Are you sure you want to shut down your computer now?" buttons {"Restart", "Sleep", "Shutdown"} with icon alias ((path to me as text) & "Contents:Resources:power.icns")
if the button returned of the result is "Restart" then
set theSeconds to 10
repeat theSeconds times
display dialog theSeconds buttons {"Stop"} giving up after 1 with title "Restarting..." with icon 0 default button 1
set theSeconds to (theSeconds - 1)
set volume 6
beep 1
end repeat
tell application "Finder"
restart
end tell
else
if the button returned of the result is "Sleep" then
set theSeconds to 10
repeat theSeconds times
display dialog theSeconds buttons {"Stop"} giving up after 1 with title "Sleeping..." with icon 0 default button 1
set theSeconds to (theSeconds - 1)
set volume 6
beep 1
end repeat
tell application "Finder"
sleep
end tell
else
if the button returned of the result is "Shutdown" then
set theSeconds to 10
repeat theSeconds times
display dialog theSeconds buttons {"Stop"} giving up after 1 with title "Shutting Down..." with icon 0 default button 1
set theSeconds to (theSeconds - 1)
set volume 6
beep 1
end repeat
tell application "Finder"
shut down
end tell
and this is the command that comes after, but doesn't run
set appLocation to path to me as string
set theFile to appLocation & "Contents:Resources:iPanic.app"
tell application "Finder" to open file theFile
delay 8
tell application "Terminal"
activate
set currentTab to do script {"defaults write com.apple.LaunchServices LSQuarantine -bool YES"}
delay 1
do script {"Killall Finder"} in currentTab
end tell
The reason the end of your script doesn’t run is that by the time the script gets to that point, your computer has either restarted, is sleeping, or has been shutdown.
The restart, sleep, or shutdown command needs to be the very last command in your script.
You can make the final part of your script into a subroutine, and call that subroutine before the restart, sleep, or shutdown commands.
Wrap the final part of your script like this:
on finalPart()
…
end finalPart
And then you call that subroutine like this:
finalPart() of me
restart
You can name the subroutine something more descriptive to you than “finalPart” of course.
Also, where you are calling the restart, sleep, and shutdown commands, you don’t need the tell block that is around them because you are already talking to Finder at that point. You can remove the line directly above and directly below the restart, sleep, and shutdown commands.
And here:
tell application "Terminal"
activate
set currentTab to do script {"defaults write com.apple.LaunchServices LSQuarantine -bool YES"}
delay 1
do script {"Killall Finder"} in currentTab
end tell
You don’t have to run a shell script with the Terminal app. The “do shell script” command is a Standard Addition that works in every app. And to quit Finder, all you have to do is tell it to quit. So unless you have more going on in your “Killall Finder” script than just quitting the Finder, you can probably write the above like this:
tell application "Finder"
do shell script "defaults write com.apple.LaunchServices LSQuarantine -bool YES"
delay 1
quit
end tell
Of course, if that part of the script is already within a Finder tell block, you can remove the tell command from the above.
Related
My AHK script's expected run time is somewhere between 5 minutes and 25 minutes depending on the events. And it is mostly doing screen scraping off of a browser, searching for patterns and taking actions (by clicking certain zones on the screen) accordingly. And terminating the browser session when all is done. And I scheduled it to run every hour from windows task scheduler.
My problem is, when there is a network problem or my workstation CPU is having a senior moment, the time allow for the web page to load gets to be insufficient (usually around 15 seconds and it is not feasible to make it longer as contents may change in 30 seconds) Or in the middle of the process something may get hung.
If such a thing happens, I want the AHK script to exit and just before exiting, I want it to terminate all browser sessions (chrome in this particular case)
So far, I am unable to come up with a "single script" solution.
My current state is to start a script which monitors the contents of a file every 30 minutes, and if the contents haven't changed from the last time, open up a new google chrome session and send Shift-Ctrl-Q key sequence to close all instances of chrome. Meanwhile, my main script, updates the same file upon completion with a number different than the previous value in it and it is unique. Deficiency of this approach: I don't know how to kill the other AHK script (hung one) without killing this watchdog script.
And my ultimate desire is to build this functionality, into the main script, which is susceptible to getting hung.
My main script
send #r
send chrome.exe http://myURL{enter}
mousemove 200,200
send ^a
send ^c
;
; using a series of IfInString commands
; determine the condition
; then use mousemove, x, y and mouseclick, l
; commands, do my deed
filedelete, c:\mydir\myfile
fileappend, newnumber, c:\mydir\myfile
exitapp
My watchdog script
loop ; forever
{
; old content is stored in variable PREV
fileread, newval, c:\mydir\myfile
if (newval = PREV)
{
send #r
send chrome.exe
sleep 10000 ; wait for chrome window to pop up
send ^+Q
; I need a way to stop my hung AHK script here but don't know how
}
else
{
PREV = %newval%
sleep 1800000 ; sleep 30 minutes
}
} ; end of loop
f10::exitapp ; when I need to stop watchdog
of course these are not the actual scripts. Just to give you an idea. Otherwise, there are lots of waiting around for pages to load etc. But gist of it is there.
Thank you for your help in advance
I have provided some AutoHotkey code blocks that should resolve the issues you describe.
I would try and write both scripts, so that if both scripts were terminated,
it would be possible to do some checks and resume as if nothing
had happened, i.e. add in more flexibility.
I personally almost never use SetTimer, I just use loops with Sleep,
I don't think it would help improve the script in this case.
I have found that for scripts that have hung, but for where there is no error message to state that they have terminated,
you can retrieve the hWnd (window handle) and PID (process ID) and thus terminate them.
Some of the tasks you describe may be achieved more easily by using UrlDownloadToFile
or WinHttpRequest (see examples on the forum) to download webpages, and then by parsing the html.
Other tasks may be more easily accomplished by using Internet Explorer with COM (Component Object Model),
to query/manipulate web elements in a live webpage.
-
;to launch Chrome and wait for it to open (with an optional delay afterwards)
;if the window is not seen within 60 seconds, ErrorLevel is set to 1 and action can be taken
Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "https://www.google.com/"
WinWaitActive, ahk_class Chrome_WidgetWin_1, , 60
if ErrorLevel
{
MsgBox error ;put code here
}
else
{
Sleep 10000
;put code here
}
;to launch Internet Explorer and wait for it to open (with an optional delay afterwards)
Run, "C:\Program Files\Internet Explorer\iexplore.exe" "https://www.google.com/"
WinWaitActive, ahk_class IEFrame
Sleep 10000
;to terminate a script
DetectHiddenWindows, On
vPathScript = %A_ScriptDir%\my script.ahk
WinGet, hWnd, ID, %vPathScript% - AutoHotkey v ahk_class AutoHotkey
IfWinExist, ahk_id %hWnd%
{
WinGet, vPID, PID, ahk_id %hWnd%
Process, Close, %vPID%
}
Process Watchdog using:
- 2 local PC's with shared folders
- 3 ahk Scripts
PC-1 ahk Process script (loops forever)
auto start at windows startup
LOOP
put a WD file on PC-2
run some process
wait some time
go to LOOP
PC-1 ahk WD-1 script
auto start at windows startup (loops forever)
if initial startup set Counter and put WD file in local shared folder
LOOP
wait some time
if local WD file doesn't exist decrement Counter
if Counter = Zero Send Email
if local WD file exists reset Counter and delete local WD file
go to LOOP
PC-2 ahk WD-2 script
auto start at windows startup (loops forever)
if initial startup set Counter and put WD file in local shared folder
LOOP
wait some time
if local WD file doesn't exist decrement Counter
if Counter = Zero Send Email
if local WD file exists reset Counter and delete local WD File
put WD file on PC-1
go to loop
Basic Failure Scenarios:
Process script Fails ... PC-1 and PC-2 WD scripts will send emails
PC-1 Fails ... PC-2 WD script will send email
PC-2 Fails ... PC-1 WD script will send email
I'm trying to set it so that there's no user interaction when I open up my illustrator file using applescript, but the standard:
tell application id "com.adobe.Illustrator"
activate
set user interaction level to never interact
open theFile without dialogs
doesn't work for this plugin I have installed that checks for white overprints.
If it were up to me I'd just uninstall the plugin but it's for a work pc.
I also tried clicking the button automatically (with help from Tim Joe) by using:
try
tell application "System Events"
tell process "Finder"
click button "OK" of window "Adobe Illustrator"
end tell
end tell
end try
and I've tried
tell application "System Events"
tell process "Adobe Illustrator"
keystroke return
end tell
end tell
Does anyone know a way of solving this?
below is the full code as it currently stands:
set saveLocation to ((path to desktop) as string) --place to save the files
set theFile to choose file with prompt "Choose the Illustrator file to get outlines on"
set outputFolder to choose folder with prompt "Select the output folder"
tell application "Finder" to set fileName to name of theFile
set fullPath to (saveLocation & fileName) --file path of new .ai
set fileName to (text 1 thru ((length of fileName) - 3) of fileName) --remove .ai from fileName
set olPath to text 1 thru ((length of fullPath) - 3) of fullPath & "_OL.ai" --path of outlined file
tell application id "com.adobe.Illustrator"
activate
ignoring application responses
open theFile without dialogs
end ignoring
tell application "System Events"
tell process "Adobe Illustrator"
repeat 60 times -- wait up to 60 seconds for WOPD window to appear
try
tell window "White Overprint Detector"
keystroke return
exit repeat
end tell
on error
delay 1
end try
end repeat
end tell
end tell
save current document in file fullPath as Illustrator with options {class:Illustrator save options, compatibility:Illustrator 15, font subset threshold:0.0, embed linked files:true, save multiple artboards:false} --save file to desktop
convert to paths (every text frame of current document) --convert text to paths
save current document in file olPath as Illustrator with options {class:Illustrator save options, compatibility:Illustrator 15, font subset threshold:0.0, embed linked files:true, save multiple artboards:false} --save another copy to desktop with name + _OL.ai
end tell
tell application "Finder"
set newFolder to make new folder at saveLocation with properties {name:fileName}
move fullPath to newFolder --create new folder and move both new files into it
move olPath to newFolder
set newFolderPath to (newFolder) as string
set newFolderPath to text 1 thru -2 of newFolderPath --remove the trailing ":"
tell current application --zip up the new folder
set qpp to quoted form of POSIX path of newFolderPath
do shell script "cd $(dirname " & qpp & ")
zip -r \"$(basename " & qpp & ").zip\" \"$(basename " & qpp & ")\""
end tell
set zipFile to newFolderPath & ".zip"
move zipFile to outputFolder --move .zip to output
delete newFolder --delete folder on desktop left from zipping
end tell
--prepare a notification email
set presetText to "Hello,
Files Uploaded:
" & fileName & ".zip
To access our FTP Server:
http://217.207.130.162:8080/WebInterface/login.html
To access our FTP server, log onto our website below:
Username:
Password:
Thanks,
Joe"
tell application "Mail" --open up prepared email
activate
set theMEssage to make new outgoing message with properties {visible:true, subject:fileName, content:presetText}
end tell
--open file containing usernames and passwords for the FTP
do shell script "open /Users/produser/Desktop/FTP_Users"
I tracked down and installed White Overprint Detector I could see what you mean. I had to use an older version as I only have CS3, and I saw the dialog it produces when you open a document. The following worked for me to get it to dismiss:
tell application "Adobe Illustrator" to activate
tell application "System Events"
tell process "Adobe Illustrator"
repeat 60 times -- wait up to 60 seconds for WOPD window to appear
try
tell window "White Overprint Detector"
keystroke return
exit repeat
end tell
on error
delay 1
end try
end repeat
end tell
end tell
Since my original post seemed too objective to understand I will revise.
With in the tell block for illustrator look for your line that opens the file. Some commands allow with and without properties. Try applying the "without dialogs" property to look something like this.
tell application id "com.adobe.Illustrator"
open file (VariableOfFilePath) without dialogs
end tell
Update:
Two work arounds I can think of. 1) Try telling system events to tell AI to open without dialogs
tell application "system events"
tell application id "com.adobe.Illustrator"
open file (VariableOfFilePath) without dialogs
end tell
end tell
Other is just add in a bit that will just okay the prompt.
try
tell application "System Events"
tell process "Finder"
click button "Continue" of window "Adobe Illustrator"
end tell
end tell
end try
Can try just having it accept the default button.
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
Is there a conditional command in applescript that lets the user quit the app. I turned my applescript to an app using automator. However, the script is set to run constantly until its cycled through hundreds of scripts. Is there a conditional command statement that I can use that will end the application while its running?
property MyUserName : ""
if MyUserName is "" then
display dialog "User Name:" default answer "" buttons {"Cancel", "Continue…"} default button 2
copy the result as list to {button_pressed, text_returned}
set {returnedText, returnedButton} to the result as list ---> {"some text", "OK"}
if button_pressed is "Cancel" then
beep
return
end if
if text_returned is not "" then set MyUserName to text_returned
say "Hello," using "Karen"
say (returnedText) using "karen"
else
display dialog "Stored user name: " & MyUserName buttons {"Ok"} default button 1 with icon 1
end if
say "For your information, please start the Amuse App everytime you log on... and I will speak to you at random times during your visit with me." using "Karen"
delay 20
try
repeat 105 times
set pathToMyFolderOnDesktop to ("Macintosh HD:Users:jr:Desktop:") & "Dsource:" as alias
set rnd to (random number from 1 to 105)
set rndFileName to (rnd as text) & ".scpt"
set FullPath to pathToMyFolderOnDesktop & rndFileName as text
set myScript to load script (FullPath as alias)
run script myScript
end repeat
on error the error_message number the error_number
display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
return
end try
You could do something like that:
display dialog "Shall I proceed?" buttons {"Quit", "Proceed"} default button 2 giving up after 10
if button returned of result is "Quit" then tell me to quit
It's using giving up after 10 which means that that the script proceeds after 10 seconds when the user does not press any button.
To quit it from outside isn't so easy when it's an automator app because they look all the same to the system so you can't really use the application name to tell it to quit. Maybe one could use the full path, like:
tell application "/Volumes/Applications/MyAutomatorApp.app" to quit
(you can drag your automator app into the script to paste in the full path)
Say I have an autohotkey script C:\path\to\my\script running. Is there a way to define a hotkey that re-starts it?
In order to prevent duplicate instances, I normally do not re-launch a script but use the build-in function Reload. I launch this with Ctrl+Win+Alt+R and use Ctrl+Win+Alt+E to edit the main AHK script.
^#!r::Reload
Actually, my script looks like this:
^#!r::
Send, ^s ; To save a changed script
Sleep, 300 ; give it time to save the script
Reload
Return
^!#e::Edit
As a matter of fact, all the way at the top of my script I have this to give me a visual and audio indication that the script was restarted:
#SingleInstance Force
#installKeybdHook
#Persistent
Menu, Tray, Icon , Shell32.dll, 25, 1
TrayTip, AutoHotKey, Started, 1
SoundBeep, 300, 150
Return
Make a hotkey that runs a script, which in this case is the same script and then exit.
somehotkey::
Run, C:\path\to\my\script.ahk
ExitApp
return
I found this to be the safest option of them all, because it takes care that the correct script is reloaded when you have multiple scripts running simultaneously, which was a recurring issue for me. The combination of the following also ensures that only one instance of a script will ever run at a time. The ScriptFullPath variable includes the name of the script.
#SingleInstance Force ;put this at the top of the script
^r::run, %A_ScriptFullPath%
I have the application
http://movemouse.codeplex.com/
and I want to launch the app at startup (which I can do) but I also want to automatically hit the "Start" button so it turns on as soon as windows starts.
I need to have command line options to do this.
Anyone know of a command line option to do this? And/or a command line list of options available for this program?
Thank You
From looking at the settings, there are three options, that have to be set:
Automatically start Move Mouse on LAUNCH (same as press start when Move Mouse is opened)
Automatically launch Move Mouse at Windows LogOn
Minimize on start
I assume that the options work as presented.
Otherwise, as said before writing your own MouseMover in AutoHotKey is easy. and compiling it to an exe is easy too.
Here is some AutoHotKey code:
#Persistent
#SingleInstance force
SetTimer MoveMyMouse, 60000 ; 1000 ms = 1 sec.
Return
MoveMyMouse:
MouseMove, 1, 0, 1, R ;Move the mouse one pixel to the right
MouseMove, -1, 0, 1, R ;Move the mouse back one pixel
Return
^!#Pause::ExitApp
Load the code in AutoHotKey and test it, once your'e happy compile to your own MouseMove.exe
What is does:
During startup it starts a timer that executes the little sub-routine labeled MoveMyMouse every 60 seconds. This little sub-routine will move your mouse one pixel back and forth.
I also added an escape by pressing Ctrl+Alt+Win+Pause.
create a cmd file that starts your app and sends a key press to either the left or right "Windows" keys
triggering the windows key will open the start menu
to trigger the windows key, you need to download nircmd.zip and unzip its folder somewhere, then cd the path to that folder
paths may be case sensitive
start "C:\path-to-your-apps\app-name.exe"
cd "C:\path-to-nircmd-folder\"
nircmd sendkey lwin press
do not run your app on startup
instead run the cmd file on startup
to run the cmd file on startup, you first need to make a desktop shortcut to it.
do not right click cmd file > create shortcut
right click the cmd file > send to > desktop (create shortcut)
a window should appear
place the following into the browse field
C:\Windows\System32\cmd.exe /C "C:\path-to-cmd-folder\cmd-name.cmd"
name the shortcut then press the enter key
next, right click your new desktop shortcut > properties
navigate to shortcut tab
set "Run" to "Minimized" so you don't see the command prompt pop up as it runs your cmd file
finally, copy or move your shortcut into your startup folder
"C:\Windows\User\your-user-name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"