the code looks like this:
echo 1. Start
echo 2. Info
echo 3. Exit
echo.
pause
set /p menuchoice=
if %menuchoice% == 1 goto startgame
if %menuchoice% == 2 goto info
if %menuchoice% == 3 exit
goto menu
The menu comes in with pressing enter once for Press any key, but at the choosing place i have to put enter twice and then it just exits, but i want it to go back to the menu. Also if its possible, i have to know: is there a way that i can make it choose something if i press the number without the confirmation enter? (sorry for bad english)
Related
I am writing a script to print a section of a bookdown online book as PDF, then move to the next section, and so on.
The print part works (the key codes are from this page):
tell application "Safari"
activate
tell application "System Events"
key code 35 using command down -- activate print menu item
end tell
delay 0.5
set i to 0
repeat while i < 15
set i to i + 1
delay 0.1
tell application "System Events"
key code 48 -- press tab 15 times
end tell
end repeat
tell application "System Events"
key code 49 -- press space
end tell
set i to 0
repeat while i < 2
set i to i + 1
delay 0.1
tell application "System Events"
key code 125 -- press down key twice
end tell
end repeat
tell application "System Events"
key code 36 -- enter
end tell
set i to 0
repeat while i < 16
set i to i + 1
delay 0.1
tell application "System Events"
key code 125 -- press tab to get to "save"
end tell
end repeat
tell application "System Events"
key code 36 -- enter to cleck on save
end tell
end tell
Problem
Now that I have printed the current section and I am back on Safari, I can click manually on the right arrow and move to the next section, but I can't manage to have the script to do that.
I have tried to add the following to the script above:
tell application "System Events"
key code 124 -- right arrow to enter the next page
end tell
Or even to "reopen" Safari, but nothing happens.
tell application "Safari"
activate
tell application "System Events"
key code 124 -- right arrow to move to the next section
end tell
end tell
How can I have AppleScript "turn the page" and move to the next section?
Also, I welcome suggestions to improve the script! I wonder if it would be easy to avoid repeating "tab" 15 times. I have looked at the Accessibility Inspector and found that "PDF" in the print menu corresponds to NSPopUpButtonCell. I have tried to use select NSPopUpButtonCell of its sheet but it did not work.
I can click manually on the right arrow and move to the next section, but I can't manage to have the script to do that.
How can I have AppleScript "turn the page" and move to the next section?
If you are trying to programmatically click the right-arrow, as shown in the image below, then the following example AppleScript code can do that:
tell application "Safari" to ¬
tell document 1 to ¬
do JavaScript ¬
"document.getElementsByClassName('fa fa-angle-right')[0].click();"
Notes:
This requires Allow JavaScript from Apple Events to be check on the hidden Develop menu.
To unhide the hidden Develop menu:
Safari > Preferences… > Advanced > [√] Show Develop menu in menu bar
Update to address:
Also, I welcome suggestions to improve the script! I wonder if it would be easy to avoid repeating "tab" 15 times.
Here is how I'd use the JavaScript from above and coded to avoid using the key code and or keystroke System Events commands, especially tabbing around the UI.
Example AppleScript code:
tell application "System Events"
tell application process "Safari"
set frontmost to true
set i to 0
repeat until (its frontmost = true)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click menu item "Print…" of ¬
menu 1 of ¬
menu bar item "File" of ¬
menu bar 1
tell its front window
set i to 0
repeat until (exists menu button "PDF" of sheet 1)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click menu button "PDF" of sheet 1
set i to 0
repeat until (exists menu item "Save as PDF" of ¬
menu 1 of menu button "PDF" of sheet 1)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click menu item "Save as PDF" of ¬
menu 1 of ¬
menu button "PDF" of ¬
sheet 1
set i to 0
repeat until (exists button "Save" of sheet 1 of sheet 1)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click button "Save" of sheet 1 of sheet 1
set i to 0
repeat while (exists sheet 1)
delay 0.1
set i to i + 1
if i ≥ 100 then return
end repeat
end tell
end tell
end tell
tell application "Safari" to ¬
tell document 1 to ¬
do JavaScript ¬
"document.getElementsByClassName('fa fa-angle-right')[0].click();"
Notes:
Since this type of AppleScript script is using UI Scripting, I have included an error handling in the form of a repeat loop to wait up to two seconds for the target UI element to become available to be acted upon for most of the targets, however the last repeat loop waits longer because it has to wait until the Save as PDF to complete. A simple delay command with an appropriate value could be used instead, but with the include delay of a tenth of a second in the repeat loops it shouldn't have to wait any longer than need be. In other words, I'd only use simple delay command if I want to slow the script down from going through the various events of the UI. It's doubtful that it would need to be adjusted, but obviously do so as/if necessary.
If the timeout is reached, the script aborts at that point without any error message. The single-line if i ≥ 20 then return statements can be turned into a full if block and include an error message via the display dialog, display alert, or display notification command as wanted.
I'm trying to automate export of my calendar on Outlook 2019 on Apple OS 10.13; I don't see the Export command in the scripting dictionary of the application. Am I missing it? Is there a better way to automate this?
What is Apple OS? Is it MacOS???
My Microsoft Outlook version is "version 16.29 (19090802)", the code below works on my MacOS 10.12.6. You can try if it works on your machine.
tell application "Microsoft Outlook"
activate
delay 0.3
end tell
tell application "System Events"
tell process "Microsoft Outlook"
set theExportAsName to "Outlook for Mac Archive.olm"
set theExportToFolder to "/Users/admin/Desktop"
--input the file name and the destination folder path you want to export as
tell menu item "Export..." of menu "File" of menu bar item "File" of menu bar 1
click
delay 0.5
end tell
--show the Export dialog (1/3), we need to deal with three dialogs totally.
--knowledge point 1: how to click menu item in APP menu bar
tell group 1 of group "What do you want to export?" of front window
if value of radio button "Items of these types:" is not 1 then
click radio button "Items of these types:"
end if
end tell
--1st we choose export items
tell group 1 of group 1 of group "What do you want to export?" of front window
if value of checkbox "Mail" is not 0 then
click checkbox "Mail"
end if
if value of checkbox "Calendar" is not 1 then
click checkbox "Calendar"
end if
if value of checkbox "Contacts " is not 0 then
click checkbox "Contacts "
end if
end tell
tell group 2 of group 1 of group "What do you want to export?" of front window
if value of checkbox "Notes" is not 0 then
click checkbox "Notes"
end if
if value of checkbox "Tasks" is not 0 then
click checkbox "Tasks"
end if
end tell
--2nd we check Calendar, uncheck all the others
click button "Continue" of front window
delay 0.5
--3rd click Continue button to go to next dialog window (2/3)
tell text field 1 of sheet 1 of front window
set value to theExportAsName
end tell
--1st we set the file name we want to use
set the clipboard to theExportToFolder
keystroke "G" using {command down, shift down}
delay 0.5
keystroke "v" using {command down}
delay 0.5
keystroke return
delay 0.5
--2nd we set the destination folder path we want to use
tell sheet 1 of front window
click button "Save"
delay 0.5
end tell
--3rd click Save button to go to the last dialog window (3/3)
tell front window
click button "Finish"
end tell
--1st click Finish button to finish this task
end tell
end tell
Note 1: the delay duration is longer than needed, I set it to big number to make the code run slowly, to make you see what happened. You can test then set smaller delay value to speed up the running.
Note 2: to these three dialog window, I all use the code below to get information about the UI elements - to see how to reference them.
tell application "Microsoft Outlook"
activate
delay 0.3
end tell
tell application "System Events"
tell process "Microsoft Outlook"
tell front window
set uiElems to entire contents
end tell
end tell
end tell
Note 3: I write some comments to explain each step of the code. You can test it on your machine. Please let me know if it helps.
I'm trying to remove (all) items from the dock. I can remove them by name like so:
tell application "System Events"
tell UI element "Launchpad" of list 1 of process "Dock"
perform action "AXShowMenu"
click menu item "Remove from Dock" of menu 1
end tell
end tell
But I'd like to pull the list of current items and iterate over them.
This stack overflow question seems to cover how to get the list. What I'd like to do is tweak the above code to operate inside of a loop. I would guess that referencing the current item of the list inside the loop would be done with "thisRecord". I think I'm misunderstanding how to convert "thisRecord" into something I can reference within system events.
set plistpath to (path to preferences folder as text) & "com.apple.dock.plist"
tell application "System Events"
set plistContents to contents of property list file plistpath
set pListItems to value of plistContents
end tell
set persistentAppsList to |persistent-apps| of pListItems
set dockAppsList to {}
repeat with thisRecord in persistentAppsList
set end of dockAppsList to |file-label| of |tile-data| of thisRecord
tell application "System Events"
tell UI element application thisRecord
perform action "AXShowMenu"
click menu item "Remove from Dock" of menu 1
end tell
end tell
end repeat
As an alternative... Here is a much more straight forwards approach to removing the persistent apps on the Dock found in the persistent-apps key of the com.apple.dock.plist file:
In Terminal, do the following to first backup the target file:
cd ~/Library/Preferences
cp -a com.apple.dock.plist com.apple.dock.plist.bak
Now to remove the persistent apps, use the following compound command:
defaults delete com.apple.dock persistent-apps; killall Dock
If later you want to restore the backup, use the following compound command:
cd ~/Library/Preferences; rm com.apple.dock.plist; cp -a com.apple.dock.plist.bak com.apple.dock.plist; killall Dock
If for some reason you need to do this with AppleScript, you can use the do shell script command to run these shell commands.
Note: In your OP you stated "I'm trying to remove (all) items from the dock." and the code you've presented only focuses on the apps stored under the persistent-apps key. There are also additional items that can show on the Dock, the first being the default persistent-others, which has the Downloads stack and other items you've added to that section. Then with macOS Mojave there is recent-apps which shows between the two aforementioned sections (by key name) on the Dock. The same premise can be use on these keys as well, substituting persistent-others or recent-apps for persistent-apps in the defaults delete ... compound command.
It would probably be wise to backup your "com.apple.dock.plist" file first. These following two lines of AppleScript code will copy your current com.apple.dock.plist file to your Desktop. This will come in handy if you want to revert your Dock icons back to the way they were before you ran the second script of this post.
set plistpath to (path to preferences folder as text) & "com.apple.dock.plist"
tell application "Finder" to duplicate alias plistpath to desktop
This AppleScript code works for me using the latest version of macOS Mojave.
set plistpath to (path to preferences folder as text) & "com.apple.dock.plist"
tell application "System Events"
set plistContents to contents of property list file plistpath
set pListItems to value of plistContents
end tell
set persistentAppsList to |persistent-apps| of pListItems
set dockAppsList to {}
-- Gets app names and adds them to dockAppsList
repeat with i from 1 to count of persistentAppsList
set thisItem to item i of persistentAppsList
set appName to |file-label| of |tile-data| of thisItem
set end of dockAppsList to appName
end repeat
-- Loops through each app in dockAppsList and removes each app from Dock
repeat with thisRecord in dockAppsList
tell application "System Events"
tell UI element thisRecord of list 1 of process "Dock"
try
perform action "AXShowMenu"
click menu item "Options" of menu 1
click menu item "Remove from Dock" of menu 1 of menu item "Options" of menu 1
on error
try
perform action "AXShowMenu"
click menu item "Remove from Dock" of menu 1
end try
end try
end tell
end tell
end repeat
I realize I could have included everything in one large repeat loop. I thought it would be better ,for purposes of this script, to separate the two looping events in the event that somewhere else in your script you may want to refer back to items of dockAppsList so instead of "removing all from the dock" you may only want to remove items 1 through 5 of dockAppsList from the dock.
I have a small task I would like to automate with Autohotkey and it looks like it is more or less directly transferable to autohotkey syntax:
1. Ctrl+v
2. Alt+tab
3. Click certain link in a window (no combo-key for this but it's always in the same place)
4. Enter (carriage return)
5. Alt+s
6. Ctrl+v
7. Enter
Now it would be nice to map this combo to something else e.g. Windows Key+Space.
What I have got so far is:
0. SetWinDelay 100 (using a connection to an remote computer)
0. SetKeyDelay 0
1. Send, ^c
1. ClipWait, 0.1
2. Send, {Alt down}{tab}
2. Send, {Alt up}
3. ?????
4. Send, {enter}
5. Send, !s
6. Send, ^v
7. Send, {enter}
Is this approximately right? Anyone up for helping me fix it or filling in the holes, so to speak :)
Another alternative to step 3, 4 and 6 would be to simply loop though the contents of the clipboard (a number string) and sending each letter of the string to keypresses? Maybe this would be the easier way
If you want to "click" on a certain position, to open a menu, you can first right click on your AutoHotKey icon and open the "window spy". This window spy will show you the mouse position. Yo can use the mouse positions to perform your actions in the active application.
Example:
SoundBeep 1000, 300 ; Wake up user
SplashTextOn, 200, 100, Script Preparations, Please Click on the person icon link. ; Show new Instructions text
WinMove, Script Preparations,, (A_ScreenWidth/2)+150, (A_ScreenHeight/2)+200 ; Move the window with the name "Script Preparations" Down and Right on the main screen
KeyWait, LButton, D ; Wait for LeftMouseButton click Down
MouseGetPos, xposE ,yposE ; Store the position where the mouse was clicked (Employee)
MouseClick, left, %xposE% ,%yposE%, 2 ; Perform a double mouse click on the captured mouse location
SplashTextOff ; Remove Text box
In this case, I first ask the user to manually click on the right location. This is only required when the position to click changes WITHIN the active window (variable tiles within the active window). Once you have the position stored, you can re-use it all throughout your script.
b.t.w. instead of using Alt+Tab, I suggest using this:
settitlematchmode, 1 ; Set search in title to start with....
settitlematchmode, Fast ; Slow is not required here. Slow is only required when hidden text needs to be found.
SwitchWindow("Microsoft Excel - 1 QRM Upload and Change Template") ; Activate the
window with the title: Microsoft Excel - 1 QRM Upload and Change Template
You could even use someting like this:
SetTitleMatchMode, 2 ; Ensure that the Title Match mode is set to 2: Find anywhere in the title
SetTitleMatchMode, Fast ; Ensure that the Title Match mode is set to FAST
winactivate, %WindowName% ; Activate the window with the title stored in the variable WindowName
WinWaitActive, %WindowName%, , 5 ; Wait up to five seconds for the screen
if ErrorLevel ; Execute this when the window is not activated within 5 seconds
{ ; Start-If Wait failed
SoundBeep 1000 , 1000 ; Warn the user
MsgBox,4097,Time Out, Script timed out while waiting for %WindowName%.`n`rYou Must manually activate %WindowName% and then continue the script by pressing OK. ; Message to user
IfMsgBox, Cancel ; Do when the user clicked on Cancel
{ ; Start-If User clicked Cancel
ExitApp ; Exit this program when the user clicked on Cancel
} ; End-If User clicked Cancel
WinWaitActive, %WindowName%, , 5 ; Try to activate the window AGAIN
if ErrorLevel ; If window can't be found
{ ; Start-If window can't be found
MsgBox,4096,Exit, %WindowName% still NOT Active. ; Warn user
ExitApp ; Exit this program when the expected window is still not found
} ; End-If window can't be found
} ; End-If Wait failed
Regards,
Robert Ilbrink
How do I repeat a navigation command in vi?
For example, I execute the command 20j which moves the cursor down 20 lines, and I tried hitting . to repeat that command, but it says "No command to repeat".
P.S. Also, what command goes to the next page in a document?
There isn't a shortcut to repeat the last navigation command - you have to retype it, or set up some sort of shortcut of your own (:map or similar).
Page up (back) is Control-B; page down (forward) is Control-F. Half-pages are Control-U (up) and Control-D (down).