Set cliclick co-ordinates relevant to UI element in AppleScript - macos

Firstly I have only been using AppleScript for the last couple of days, so am really new to it and have no real other scripting knowledge, so forgive me if this comes across as something trivial or I ask lots of follow up questions.
I have an element that I would like to right click on using cliclick, but the location of the element moves depending upon other windows and sidebars being open
I would like to use
position of button 1
to return the correct co-ordinates so I would like to be able to use maths to add to the X,Y co-ordinates in cliclick so it would be something like this
position of button 1
do shell script "../cliclick c:x+25,y+5"
is this possible or is it something that I will have to using another language? I am thinking the final project will be compiled into an application using Xcode and will be hiring someone for this, am just trying to make their life as easy as possible beforehand.

Ok after a bit of fumbling around the internet I found something else and then fiddled with it a bit and finally solved the above using the following
tell UI element 1 of toolbar 1 of toolbar 3 of the front window
set p to position
set x to item 1 of p
set y to item 2 of p
end tell
do shell script "/Users/dave/_app-dev/cliclick c:" & x & "," & y & ""
Just incase anyone else looks for it here is the answer

A more compact equivalent code:
set {x, y} to position of button 1 of toolbar 1 of toolbar 3 of window 1
do shell script "/Users/dave/_app-dev/cliclick c:" & x & "," & y & ""

Related

How to use AppleScript with Calculator.app to Automatically select the number of Decimals

Every time I enter a number in Calculator.app I need to go the top menu and select:
View >> Decimal Places >> [0 , 15] And choose the number of decimals.
Is there a way to make an AppleScript that will automatically do so, based on the number I entered?
My input ways are:
by pasting the number
by typing the number
For the pasting part, if you PASTE a number ending in 0, Calculator.app won't show the 0, so it will show one decimal less than the reality. I don't know if this could be also overcome.
Example, if you paste: 12.30 it will show 12.3
if you type: 12.30 it will show 12.30
Building on the addendum to my answer to your other question How can I copy result from Calculator.app to the clipboard using AppleScript, here is a way the trap ⌘V in Calculator and set Calculator > View > Decimal Places to the number of decimal places of the number on the clipboard.
What the example AppleScript code does:
When pressing ⌘V in Calculator the contents of the clipboard is set to a variable and if it contains a decimal point it sets the Calculator > View > Decimal Places menu to the number of decimal places in the number, then keystrokes the number into Calculator. Note that keystroking the number would probably not require you to set the number of decimal places, however I'll leave that up to you to decide.
Example AppleScript code:
set theNumberToType to the clipboard as text
if theNumberToType contains "." then
set theNumberOfDecimalPlaces to count characters ((offset of "." in theNumberToType) + 1) thru -1 of theNumberToType
if theNumberOfDecimalPlaces is greater than 15 then set theNumberOfDecimalPlaces to 15
else
set theNumberOfDecimalPlaces to -1
end if
tell application "Calculator" to activate
tell application "System Events"
if theNumberOfDecimalPlaces is greater than -1 then
click (first menu item of menu 1 of menu item "Decimal Places" of menu 1 of menu bar item "View" of menu bar 1 of process "Calculator" whose name is theNumberOfDecimalPlaces)
end if
delay 0.1
keystroke theNumberToType
end tell
The example AppleScript code above is saved as CalculatorSetDecimalPlaces.applescript in: ~/.hammerspoon/Scripts/, like in my other answer to your other question.
Example Lua code:
-- Create a hotkey used to trap the command v shortcut and disable it.
-- It will then be enabled/disabled as Calculator is focused/unfocused.
-- When enabled and command v is pressed it runs the AppleScript script.
local applicationCalculatorCommandVHotkey = hs.hotkey.bind({"cmd"}, "V", function()
local asFile = "/.hammerspoon/Scripts/CalculatorSetDecimalPlaces.applescript"
local ok, status = hs.osascript.applescriptFromFile(os.getenv("HOME") .. asFile)
if not ok then
msg = "An error occurred running the CalculatorResultToClipboard script."
hs.notify.new({title="Hammerspoon", informativeText=msg}):send()
end
end)
applicationCalculatorCommandVHotkey:disable()
-- Add the following two line respectively before or after the lines
-- applicationCalculatorEnterHotkey:enable()
-- applicationCalculatorEnterHotkey:disable()
-- in either of the two methods presented in my other answer.
applicationCalculatorCommandVHotkey:enable()
applicationCalculatorCommandVHotkey:disable()
Notes:
With the example Lua code, as coded, the behavior of the ⌘V keyboard shortcut is only trapped and modified to trigger the example AppleScript code while Calculator has focus. The ⌘V keyboard shortcut should work normally in all other applications.
The example Lua code above is added to the ~/.hammerspoon/init.lua file from my other answer to your other question.
The example Lua code and API's of Hammerspoon and AppleScript code, shown above, were tested respectively with Hammerspoon and Script Editor under macOS Mojave and macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Update using only Lua without AppleScript
The following example Lua code eliminates the use of AppleScript from both this answer and the code in the other answer to your other related question.
Overwriting the existing code, copy and paste this into your ~/.hammerspoon/init.lua file, save it and run Reload Config from the Hammerspoon menu on the menu bar.
I have performed all the various actions and calculations discussed in the comments and had no issues occur with this new code. Hopefully this will eliminate any issues you were having.
This should also run faster now that it does have to process AppleScript code as well.
Note that as coded it is not automatically reloading the configuration file as that just should not be necessary.
-- Create a hotkey used to trap the enter key and disable it.
-- It will then be enabled/disabled as Calculator is focused/unfocused
-- When enabled and the enter key is pressed it presses = then command C.
local applicationCalculatorEnterHotkey = hs.hotkey.bind({}, "return", function()
-- Press the '=' key to finish the calculation.
hs.eventtap.keyStroke({}, "=")
-- Copy the result to the clipboard.
hs.eventtap.keyStroke({"cmd"}, "C")
end)
applicationCalculatorEnterHotkey:disable()
-- Create a hotkey used to trap the command v shortcut and disable it.
-- It will then be enabled/disabled as Calculator is focused/unfocused.
-- When enabled and command v is pressed it runs the Lua code within.
local applicationCalculatorCommandVHotkey = hs.hotkey.bind({"cmd"}, "V", function()
-- Get the number copied to the clipboard.
local theNumberToType = hs.pasteboard.readString()
-- See if there is a decimal in the number
-- and if so how many decimal places it has.
local l = string.len(theNumberToType)
local s, e = string.find(theNumberToType, "%.")
if s == nil then
theNumberOfDecimalPlaces = -1
elseif l - s > 15 then
theNumberOfDecimalPlaces = 15
else
theNumberOfDecimalPlaces = l - s
end
-- Set the number of decimal places to show.
if theNumberOfDecimalPlaces > -1 then
local calculator = hs.appfinder.appFromName("Calculator")
local vdpn = {"View", "Decimal Places", theNumberOfDecimalPlaces}
calculator:selectMenuItem(vdpn)
end
-- Type the number into Calculator.
hs.eventtap.keyStrokes(theNumberToType)
end)
applicationCalculatorCommandVHotkey:disable()
-- One of two methods of watching Calculator.
--
-- The other is below this one and commented out.
-- Initialize a Calculator window filter.
local CalculatorWindowFilter = hs.window.filter.new("Calculator")
-- Subscribe to when the Calculator window is focused/unfocused.
CalculatorWindowFilter:subscribe(hs.window.filter.windowFocused, function()
-- Enable hotkeys when Calculator is focused.
applicationCalculatorCommandVHotkey:enable()
applicationCalculatorEnterHotkey:enable()
end)
CalculatorWindowFilter:subscribe(hs.window.filter.windowUnfocused, function()
-- Disable hotkeys when Calculator is unfocused.
applicationCalculatorCommandVHotkey:disable()
applicationCalculatorEnterHotkey:disable()
end)
-- Alternate method to wait for Calculator and enable/disable the hotkey.
--
-- Uncomment below method and comment the above method to test between them.
-- The opening '--[[' and closing '--]]' and removed from below added to above.
--[[
function applicationCalculatorWatcher(appName, eventType, appObject)
if (eventType == hs.application.watcher.activated) then
if (appName == "Calculator") then
-- Enable hotkeys when Calculator is activated.
applicationCalculatorCommandVHotkey:enable()
applicationCalculatorEnterHotkey:enable()
end
end
if (eventType == hs.application.watcher.deactivated) then
if (appName == "Calculator") then
-- Disable hotkeys when Calculator is deactivated.
applicationCalculatorCommandVHotkey:disable()
applicationCalculatorEnterHotkey:disable()
end
end
end
appCalculatorWatcher = hs.application.watcher.new(applicationCalculatorWatcher)
appCalculatorWatcher:start()
-- appCalculatorwWatcher:stop()
--]]

(NO mystery:) How Automator transfers "invisible" information to Script-Editor

( I have been pushed to an obvious answer to my "Mystery" from a friendly member here.
I feel a little ashamed not to have found this solution myself, but will leave this posting online if there aren't too many irritated folks around. Maybe someone else can learn from this … apologies to every "know-it-alls"!)
I have recorded these actions with Automator:
– Cmd-tab to bring TextEdit to front (must have some lines typed)
– Pressed left arrow 7 times using shift down
– Stopped the recording
Next I selected and copied (Cmd-c) all action icons in Automator's "Record my actions" window.
I switched to Script-Editor and pasted (Cmd-v) them into a new window.
Then, I repeated above recording with 3 times UP arrow and copied icons into another new window.
I took only the two "set uiScript to …" lines and appended them in the first script.
THEY READ IDENTICAL:
set uiScript to "keystroke \"\t\" using command down"
my doWithTimeout(uiScript)
set uiScript to "keystroke \"\" using shift down" -- 7 times left-arrow
my doWithTimeout(uiScript)
set uiScript to "keystroke \"\" using shift down" -- 3 times up-arrow
my doWithTimeout(uiScript)
on doWithTimeout(uiScript)
set endDate to (current date)
repeat
try
run script "tell application \"System Events\"\n" & uiScript & "\nend tell"
exit repeat
end try
end repeat
end doWithTimeout
(To make resulting code more readable I omitted error code and "delays".)
Now, if I disable one of the "keystroke" lines (=> --my doWith…) the script somehow knows that it either has to do Shift-leftArrow 7 times OR Shift-upArrow 3 times.
I tried this after computer restart, even copied the code from this web page and pasted it into a new Script Editor window – it still knew what to do!
HOW CAN THAT BE ???
My only idea is: there must be some internal Applescript database that recognises content even if copied/pasted.
Does anybody know ?
Only if I re-write the code identically will there happen NOTHING – until I copy the first line of either recording ("set uiScript to …"). So the information must be linked to this first line somehow.
(BTW: first two lines that bring TextEdit –or, e.g. a Finder window– to the front work only from opened Script Editor; you have to bring TextEdit to the front yourself, if you start the saved-as-program script from Script-Editor's menu icon subfolder. Nevertheless the script won't work without them…)
It "knows" because the keystrokes are in the string - keystrokes using control type keys (such as arrow keys) just don’t have a text representation, so they wind up being invisible. Some text editors such as BBEdit can show these invisible characters, but they don’t show up in the Script Editor.
Apple has obviously made Automator's Watch Me Do action able to capture some of these control keys in a string, but for the rest of us it is more difficult, since the control keys will actually perform their function when pressed. If you need to use these kinds of keys, the key code command can be used, since it refers to the actual keyboard key, for example:
tell application "System Events"
repeat 3 times
key code 126 using shift down -- up arrow
end repeat
end tell
As the answer is quite simple but may still be interesting to some, I'll explain it shortly:
red_menace (in his comment above) pointed out that strings may contain invisible elements (like arrow keys), therefore I next checked the obvious:
If you "cursor-walk" along the "set uiScript …" strings the cursor will actually "pause" for 7 or 3 "steps" respectively on its way.
I hadn't thought/heard of any "invisible" string-chars (apart from obvious ones in Word etc.).

How to Move the mouse using VBScript

I am trying to move the mouse using VBScript. I tried to use Sendkeys "{CLICK LEFT , x , y}" and Sendkeys "{MOVETO, 10 , 20}" but it isn't working i also tried to use MouseKeys, so i could move it with the keyboard and therefore use Sendkeys to activate it but it also did not work (the mousekeys numpad is not moving the mouse). I've tried everything I know and what i could research elsewhere, so now I hope one of you can answer this for me. thxs
One possible way to move the mouse is:
Dim Excel: Set Excel = WScript.CreateObject("Excel.Application")
Excel.ExecuteExcel4Macro "CALL(""user32"",""SetCursorPos"",""JJJ"",""xxx"",""yyy"")"
-->xxx = X Position
| yyy = y Position
The only disadvantage is that you need Microsoft Excel to run this script...
Hope I could help you
VBScript can't do this natively. You'd have to do some calls to the Windows API or some other library that can do this for you.
As an alternative, you may want to consider a different scripting language, like AutoHotKey which can do this in one simple line of code for you.

Does someone know the short cut for this button in xcode

right bottom in the screen shot
I know I can find it in Cmd+, but I don't know its name, hide something??
in Xcode 6
It's not exactly what you want but maybe it could help. You have:
cmd + shift + Y to show/hide debug area
cmd + shift + C to activate console

Send windows key in vbs?

I've looked everywhere for this, is it actually possible to do? If you press WIN + LEFT ARROW it will mount your selected window to the left of your screen, and that's exactly what I'm trying to do.
I have tried things like:
shl.sendkeys "#{LEFT}"
Because I found out that # could be for WIN just like + is for Shift and % is for Alt.
This may be a stupid question but it would help me out so much. Thank you for you time
Set Keys = CreateObject("WScript.Shell")
Keys.SendKeys("^{Esc}")
This should work, it Simulates the push of the Windows key by pushing the CTRL ^ and the ESC {Esc}keys, i don't know how to make the window mount to the left of the screen
VBScript's SendKeys doesn't support the Windows key.
You can use AutoIt instead:
Send("{#Left}")
Oh, so you want to mount the window to the left, I currently don't think there's a way to do that, but there's another shortcut for start menu, as well as windows key, it's Ctrl+Esc, ^{ESC}, you could possibly run the onscreen keyboard, then somehow tab over to the key, but it would be hard, also how about trying Alt+Space, %{SPACE}, to click the window icon then, M for move, then set the cursors position to the left of the screen, somehow? I don't currently need help moving the cursor in V.B.S., but you could Google it. Hash symbol is actually typing the symbol #, hash, tested with this. I also got help on the send-keys function, here. I have way to much researching, but I still can't find a answer. Some-one also said using "Auto hotkey", a third party program, but I don't really trust programs that can take control over your whole PC.
So I guess your options are:
Alt+Space to click window icon, then M for move, then move cursor to left side of the screen
Auto HotKey, although I don't recommend it.
Other commands, but SendKeys, in VBS,
Running a file, built in a different language to do so.
Or well, waiting-for / asking Microsoft to add it in, in the next version of Windows?!
a very late response, but here is my working effort
Dim shellObject : Set shellObject = CreateObject("WScript.Shell")
Extern.Declare micVoid, "keybd_event", "user32.dll", "keybd_event", micByte, micByte, micLong, micLong
Const KEYEVENTF_KEYUP = &H2
Const VK_LWIN = &H5B
Wait 5
Call Extern.keybd_event(VK_LWIN,0,0,0) 'press win key
Wait 5
shellObject.SendKeys "({UP})" 'right aline the window. TODO Error handlng
Wait 5
Call Extern.keybd_event(VK_LWIN,0,KEYEVENTF_KEYUP,0) 'up win key
Set shellObject = Nothing
try this
wshshell.sendkeys "{lwin}"

Resources