How can I detect whether something is selected with AppleScript? - text

I'm using some software called controllermate with allows you to customize the behavior of buttons on a keyboard or mouse. One of the 'building blocks' you can use in a particular function in an apple script. I would like to create a custom behavior for my mouse such that a certain button will execute CMD+C if something that can be copied is currently selected, but otherwise execute a different keyboard shortcut. It seems I will have to use an appleScript in order to determine whether text is selected, but after reading other some other people's solutions online I was unable to figure out how to implement it myself. Thanks for any help.

Here's a solution:
Put an empty string to the clipboard, do CMD+C, check the clipboard.
set the clipboard to ""
tell application "System Events" to keystroke "c" using command down
delay 0.2
set b to false
try
set b to (the clipboard as string) is not ""
on error -- error when the clipboard contains a custom type (like a copy in the Photos Application)
set b to true
end try
if b then -- the clipboard does not contains an empty string
-- *** script to execute a different keyboard shortcut ***
--tell application "System Events" to keystroke someChar using someModifier
end if

Related

Changing the window style of a shelled program

In Excel VBA, is it possible to change the window style of a shelled program?
Here's the situation in detail:
At the click of a command button, the user should be able to load a third-party scanning program. The software path and file are specified in a named cell called settings_scanningsoftware, as you can see below. However, I'm using the Shell program, which doesn't wait for the third-party software to load before continuing. If it did, my life would be easier. For that reason, I throw up a dialog box in Excel telling the user to wait for the software to load and then ready the item to be scanned before they click Ok. And, since I throw up a dialog box, I load the scanning program minimized so they can see the dialog box (instead of it being hidden behind the software window and they're wondering why nothing's happening). With me so far?
However, once they click Ok, I want Excel to give control to the scanning software and give it a regular-sized window. There's my problem: once the software is loaded minimized, I can't seem to change the window style to give it a regular window.
Here's my code:
On Error Resume Next
Dim ScannerShell As Integer
ScannerShell = Shell(Range("settings_scanningsoftware").Value, **vbMinimizedNoFocus**)
If (ScannerShell <> 0) Then
Dim MessageBoxValue As Integer
MessageBoxValue = MsgBox("When you're ready to scan the receipt, click Ok.", vbOKCancel, "Scan receipt")
If (MessageBoxValue = vbOK) Then
ScannerShell = Shell(Range("settings_scanningsoftware").Value, **vbNormal**)
AppActivate ScannerShell, False
SendKeys ("{TAB}{TAB}{TAB} ")
End If
End If
Notice I've got the vbMinimizedNoFocus bolded above so that the software loads minimized and the user can see the upcoming dialog box. Once they click Ok, though, the software's window should be normalized. But Excel won't do me that favour.
Is there a way to change the window style after it's loaded?
Never mind the SendKeys command. I put that in there to get the scanner to automatically scan once everything is in place (the key sequence is incomplete, but one problem at a time). I know SendKeys is not a great way of sending commands to a program, but I'll figure that out once I get this window style thing fixed.
Does anyone have any solutions for me? Thanks.

Applescript - Repeat until static text is not equal to value

I literally only started using Applescript today trying to write macros for an audio program I use so I really don't know a lot and am stumbling through this.
All I need to do is monitor when a certain window is open within the software and then most importantly, when it closes.
When I export a file from my software, a window comes up image of window The window has no identifier other than the static text "Bouncing..." In the screenshot there is a cancel button, but this doesn't apply to every instance of the window (depends on what options are selected) so it's the only thing I have to work with.
So far I have this:
tell application "System Events"
tell process "Pro Tools" to set myvalue to value of every static text of front window
repeat with i from 1 to count of myvalue
if (item i of myvalue = "Bouncing...") then repeat
end repeat
delay 1
end repeat
end tell
Testing this with a popup instead of the repeat stating it has found the static text works. So I'm halfway there.
All I need to do is to repeat this entire process over and over until the window disappears. Obviously when "myvalue" is "Bouncing..." I need to go back to the start, collect the static text values from the window again and then only exit the loop when the static text value of "Bouncing..." doesn't exist.
Currently it seems to exit the loop before the window has closed
Thanks for any help given here, I've spent hours on this and I'm just not knowledgeable enough to figure it out.
Found another way around it. It seems it does default to "window 1":
try
tell application "System Events"
repeat while (value of first static text of window 1 of application process "Pro Tools" = "Bouncing...")
delay 1
end repeat
end tell
end try

Control several Browsers with Applescript

Is there any way to control 2 different safari browser windows with Applescript at the same time und switch between them? Something like a unique ID to be sure which commands are for which safari window.
something like...
tell application "Safari" id 1
activate
....
end tell
:-)
thanks for your help.-
Windows in AppleScript are code objects, so if you make a reference to one, e.g.:
tell application "Safari"
set aWindow to window 1 -- sets a reference to the current front window
end tell
AppleScript will remember that particular window even if you move it, hide it, put it in the background, minimize it... Until you reset the variable or close the window, you can be sure that aWindow will point to that particular window. In fact, this works the same for tabs. You can say:
tell application "Safari"
set aTab to tab 3 of window 1 -- sets a reference to the third tab of the current front window
end tell
and even if you move that tab to a different window, AppleScript should still track it. Likewise, if you have a window pointing to the Google Search engine, you can say:
tell application "Safari"
set aWindow to window "Google" -- sets a reference to the Google window
end tell
and then if you navigate to a different webpage, the variable aWindow will still point at the same window. Neat, huh?
So all you need to do is create a couple of variables for your two windows, and you can switch back and forth as you like:
tell application "Safari"
-- assuming you have a 'Google' window and a 'Yahoo' window open
set firstWindow to window "Google"
set secondWindow to window "Yahoo"
set URL of document of firstWindow to "https://stackoverflow.com"
set index of secondWindow to 1
set miniaturized of firstWindow to true
end tell
Windows are identified by their absolute Id. You can see ID's with :
tell application "Safari" to set myWindow to every window
It gives you list of all open windows of Safari like :{window id 4519, window id 4426, window id 4514}. There are 3 open windows !
However to switch between 2 Safari windows, you must use their number: 1 is the frontmost, 2 is behind, 3 is behind the 2,...
You can tell the System Events to make number 2 before number 1. Doing so, old 2 becomes 1 and the 1 becomes 2.
tell application "System Events" to perform action "AXRaise" of window 2 of process "Safari"
if you just have 2 windows, the line above will keep switching between your 2 Safari windows, each time making the second window the frontmost one.

How to select a check box in a dialog box that pops open when mounting a drive?

I'm trying to launch a dialog box with a checkbox already selected, but no amount of research is showing me how to do this. Included is my script that brings up the dialog box:
mount volume "smb://xyz-server/user-share/servervolume"
mount volume "smb://xyz-server/useradmin-share/servervolume"
There is no checkbox dialog in Applescript's Standard dialogs. You can use a Choose from List here, perhaps? Something like:
property serverList : {"smb://xyz-server/user-share/servervolume", "smb://xyz-server/useradmin-share/servervolume"}
set serverChoice to (choose from list serverList with prompt "Please select the volume(s) to mount" default items (item 1 of serverList) OK button name {"Mount"} cancel button name {"Cancel"} with multiple selections allowed)
repeat with thisPath in serverChoice
tell application "Finder"
mount volume thisPath
end tell
end repeat
Yea sorry, but there is no checkbox featured in simple AppleScript dialog boxes, etc, but you could use a dialog box with custom buttons eg.
set isOver13 to the button returned of (display dialog "Are you 13 or older?" buttons {"Cancel", "No", "Yes"})
(returns "Yes" or "No" for the button selected)
or like "jweaks" said, you could also use something like choose from list, eg.
set isOver13 to item 1 of (choose from list {"Yes", "No"})
(returns "Yes" or "No" for the item choosen)
but if you desperatly want to use a checkbox then, there is 1 other way :-o
you will need to cross over to cocoa-applescript (ASObjC), this is basicly the next step in applescript coding, ASObjC is pretty much Objective-C actions, commands etc... but in AppleScript coding language :o. To do this, you need to get Xcode, and look up AppleScript Objective-C tutorials ;)
Hope this Helped :D
The window belongs to the process NetAuthAgent, you can click the checkbox only with GUI scripting.
tell application "System Events"
repeat until exists window 1 of process "NetAuthAgent"
delay 0.2
end repeat
tell process "NetAuthAgent"
click checkbox 1 of window 1
end tell
end tell

Contextual menu Mac OS X - Pasting a text string

I would like to know if it is possible to set up a contextual menu option that allows me to select a string, e.g. from BBEdit or TextEdit etc., and after selecting it > right click > call Contextual menu item > Paste/Replace with string of text.
For example, if I had a text file open and wanted to select the word "blue" I would like the contextual menu item to be able to replace that with "red."
Yes, this is like copy and pasting, but there's a reason I am asking for this specifically and it would save me tons of time at my job.
I am not an experienced programmer, so consider me to be a complete newbie.
I want to do this in Automator. I know all the steps to get to the building part, just don't know which Utilities item to use for something like this.
I did figure out how to do it with a keyboard shortcut, though I'd also like to have a universal right-click contextual menu option. I used to use some app to do this for me in the old days.
Here's what I did. Go to Automator, select "no input" and "any application" above the actions window.
On the left, click on Utilities, drag "Run AppleScript" into the window on the right.
Insert, into the middle:
tell application "System Events"
keystroke "Text You Want To Input"
end tell
Save it, then go to System Preferences » Keyboard » Keyboard Shortcuts > Services, and find your saved title. Create a keyboard shortcut.

Resources