I want to use applescript to do a periodic (every second) check to see if a specific bluetooth devices is connected, and if so, to flash up a quick notification. To frame it, I want a popup when my Airpods connect, since sometimes when I pull them out, the connect to my computer, and sometimes to my iPhone.
I've got everything figured out, except for the bluetooth check part. I've used this as a starting point, but can't get it to work. Any help would be appreciated.
repeat
set statusOld to checkStatus()
set statusNew to checkStatus()
repeat while statusOld is equal to statusNew
delay 1 --for 1 second checks
set statusNew to checkStatus()
end repeat
if statusNew is true then
display dialog "Device Added - put some real code here"
else
display dialog "Device Removed - put some real code here"
end if
end repeat
on checkStatus()
(*Delete the 2 lines below when done testing*)
--set myString to button returned of (display dialog "Connected?" buttons {"Yes", "No"})
--set myString to "name: DR-BT101 Connected: " & myString
(*uncomment line below when done testing*)
set myString to do shell script "system_profiler SPBluetoothDataTyp"
--initial check if it's not even there
if myString does not contain "Christian’s AirPods" then
return false
else
--find out if connected/disconnected
set AppleScript's text item delimiters to "name:"
set myList to the text items of myString --each item of mylist is now one of the devices
set numberOfDevices to count of myList
set counter to 1
repeat numberOfDevices times --loop through each devices checking for Connected string
if item counter of myList contains "Christian’s AirPods" then
if item counter of myList contains "Connected: Yes" then
return true
else if item counter of myList contains "Connected: No" then
return false
else
display dialog "Error Parsing" --this shouldn't happen
end if
end if
set counter to counter + 1
end repeat
end if
end checkStatus
You're missing the e:
set myString to do shell script "system_profiler SPBluetoothDataType"
^
I'm working on something similar. This seems to work well on macOS Mojave:
use framework "IOBluetooth"
use scripting additions -- https://stackoverflow.com/a/52806598/6962
on isDeviceConnected(substring)
repeat with device in (current application's IOBluetoothDevice's pairedDevices() as list)
if device's isConnected and (device's nameOrAddress as string) contains substring then return true
end repeat
return false
end isDeviceConnected
-- Usage example:
isDeviceConnected("AirPods")
I combined it with a launch agent like this: https://gist.github.com/henrik/3d4c622a5567cdf2bf461352f48ad4dd
Related
Private Sub btnBillSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBillSearch.Click
If Me.txbBILL_NR.Text = "" Or Me.txbBILL_NR.TextLength >= 4 Then
Try
Search()
If Me.bsBillGrid.Current IsNot Nothing Then
Me.dgvBill.Focus()
End If
Catch ex As Exception
Utility.ExceptionManager.HandleException(ex)
End Try
Else
MessageBox.Show("enter at least 4 numbers")
End If
End Sub
I have this method for search button in my app.
So, I already wrote the If condition for where user enters a minimum number of characters is 4 to select entries.
I need to one more validation for % sign. I figured out, that is kinda startWidth(), but I do not understand how I can get those situations:
I think about using startWith/endWith or regex maybe.
Could anyone please give me some advice where to look at.
user adds “%” sign after txbBILL_NR - the system does not add additional “%” in the background,
txbBILL_NR with identical part number are found and reflected in the list.
the user has not added the “%” sign after the txbBILL_NR - the system adds an additional “%” in the background,
txbBILL_NR with identical no. parts are found and listed.
The "%" character does not need to be represented by the user;
3.user added “%” sign at the beginning or middle of txbBILL_NR - the system does not add an additional “%” in the background,
txbBILL_NR with identical part number are found and reflected in the list (no changes required);
The "%" system only needs to be marked if the user has entered at least 4 characters in the txbBILL_NR field (assuming that the invoice number cannot be shorter than 4 characters.
If the user has entered at least 4 characters (whether they are starting characters or from the middle) and "%" is added to the beginning or middle of these symbols, the system does not add an additional "%" in the background
the invoice (s) with the identical part (symbol) part are found and reflected in the list.
'Get raw user entry.
Dim searchText = txbBILL_NR.Text
'Get user entry without wildcards.
Dim cleanSearchText = searchText.Replace("%", String.Empty)
If cleanSearchText.Length = 0 OrElse cleanSearchText.Length >= 4 Then
If searchText = cleanSearchText Then
'There are no wildcards so append one.
searchText &= "%"
End If
'Use searchText here.
End If
I think this function meets your requirements.
Private Function SearchCriterium(ByVal Crit As String) As String
' 295
Const Mark As String = "%"
Dim Fun As String ' function return value
Fun = Crit
Do While (Len(Fun) >= 4) And (Right(Fun, 1) = Mark)
Fun = Left(Fun, Len(Fun) - 1)
Loop
If Len(Fun) < 4 Then
MsgBox "Enter at least 4 characters.", _
vbExclamation, "Invalid search crterium"
SearchCriterium = Crit
Else
If InStr(Fun, Mark) = 0 Then Fun = Fun & Mark
End If
SearchCriterium = Replace(Fun, Mark, "*")
End Function
Observe that it replaces the % sign with VBA's wild card character *. You can remove that feature if you don't need it at that point.
The function removes any existing trailing placeholder and then adds one if none exists elsewhere in the string. Here is a list of the tests I ran.
Private Sub Test_SearchCriterium()
Debug.Print SearchCriterium("Item")
Debug.Print SearchCriterium("It%em")
Debug.Print SearchCriterium("%Item")
Debug.Print SearchCriterium("Ite%")
Debug.Print SearchCriterium("It%%")
End Sub
I want to create a Mac app similar to Textexpander or Atext. Both these applications allow the user to define snippets along with their respective trigger words. Typing the trigger words in any app, replaces that trigger word with the actual snippet defined.
I presume that the app listens to all strings being typed in any app and when it detects a string matching one of the trigger words defined, it replaces it with the snippet.
Is that how it actually works, or is there some other way?
Make two fields. In field 2 put something like:
time xyz
come ABC
In the script of field 1:
on textChanged
if the last char of me = space then
put the last word of me into temp
if temp is in fld 2 then
repeat for each word tWord in fld 2
put the last word of line lineOffset(temp,fld 2) of fld 2 into the last word of me
exit repeat
end repeat
end if
select after text of me
end if
end textChanged
Now type into fld 1, you know, "Now is the time for all good men to come to the aid of their country". This can be better done with an array, but the concept may be more accessible here.
This is a better handler, since it will not react to the trigger word:
on textChanged
if the last char of me = space then
put the last word of me into stringOfInterest
put fld 2 into dataToSearch
if stringOfInterest is in dataToSearch then
repeat for each line tLine in dataToSearch
if word 1 of tLine = stringOfInterest then
put word 2 of tLine into the last word of me
exit repeat
end if
end repeat
end if
select after text of me
end if
end textChanged
HI I am having trouble getting this code to work properly. Here is the code:
property firstRow : 2051
property lastRow : 5584
set r to firstRow
-- highly recommended
-- close all of Safari's windows before...
tell application "Safari"
close windows
end tell
-- loop through the given row numbers
repeat until r is (lastRow + 1)
-- get the search value for Safari auto completion (column J)
try
tell application "Microsoft Excel"
tell active sheet
set searchTerm to string value of range ("J" & r) of active sheet
end tell
end tell
on error
-- no document open, exit the script
return
end try
-- open Safari and make a new window
tell application "Safari"
activate
make new document with properties {URL:""}
delay 0.5
set pageLoaded to false
end tell
-- type the search value into the address field and hit return (aka select and open the first proposal)
tell application "System Events"
-- here with Safari 6.1 text field 1 of group 2 of tool bar 1 of window 1 points to the URL field
set focused of text field 1 of group 2 of toolbar 1 of window 1 of process "Safari" to true
delay 0.5
keystroke searchTerm
delay 1.5
keystroke return
end tell
-- let open Safari the suggested web page and read out the finally used URL
tell application "Safari"
repeat while not pageLoaded -- keep doing this loop until loading complete
delay 5
if (do JavaScript "document.readyState" in document 1) is "complete" then
set pageLoaded to true
else
-- not sure if this second else is needed, why not just wait until the first access has finished...
-- close document 1
-- make new document with properties {URL:""}
-- tell application "System Events"
-- delay 1.5
-- set focused of text field 1 of group 2 of tool bar 1 of window 1 of process "Safari" to true
-- delay 1.5
-- keystroke searchTerm
-- delay 1.5
-- keystroke return
-- end tell
end if
set thisULR to "NO SITE"
end repeat
try
set thisURL to URL of document 1
on error
set thisURL to "NO SITE"
end try
close document 1
end tell
-- write the result into the cell next to the key word (column K)
tell application "Microsoft Excel"
if thisURL ≠ "NO SITE" then
tell active sheet
make new hyperlink of cell ("K" & r) with properties {address:thisURL, name:thisURL}
end tell
else
tell active sheet
make new cell ("K" & r) with properties {name:"NO SITE"}
end tell
end if
end tell
set r to r + 1
end repeat
I am having trouble getting the code to not crash if there is no URL saved as variable thisURL.
However, it is still crashing. It often says thisURL is not defined and then it stops the script from going to the next r value instead of adding "NO SITE" to the cell. Not sure why its not working.
It was a big chaos with all your end telland end ifetc. Another thing is that I don't understand why you need the second nested repeat-loop...
But I think I figured it out: You want to
read a value from an excel sheet
pretend to type the read out value into Safari's address bar
use the autofill and read out the URL of the found site
write the result into the adjacent excel cell
After your post edit I edited the code to this:
property firstRow : 62
property lastRow : 5584
set r to firstRow
-- highly recommended
-- close all of Safari's windows before...
tell application "Safari"
close windows
end tell
-- loop through the given row numbers
repeat until r is (lastRow + 1)
-- get the search value for Safari auto completion (column J)
try
tell application "Microsoft Excel"
tell active sheet
set searchTerm to string value of range ("J" & r) of active sheet
end tell
end tell
on error
-- no document open, exit the script
return
end try
-- open Safari and make a new window
tell application "Safari"
activate
make new document with properties {URL:""}
set pageLoaded to false
end tell
-- type the search value into the address field and hit return (aka select and open the first proposal)
tell application "System Events"
-- here with Safari 6.1 text field 1 of group 2 of tool bar 1 of window 1 points to the URL field
set focused of text field 1 of group 2 of tool bar 1 of window 1 of process "Safari" to true
keystroke searchTerm
delay 1.5
keystroke return
end tell
-- let open Safari the suggested web page and read out the finally used URL
tell application "Safari"
try
repeat while not pageLoaded -- keep doing this loop until loading complete
delay 10
if (do JavaScript "document.readyState" in document 1) is "complete" then
set pageLoaded to true
end if
end repeat
set thisURL to URL of document 1
close document 1
on error
set thisURL to "NO SITE"
try
close windows
end try
end try
end tell
-- write the result into the cell next to the key word (column K)
tell application "Microsoft Excel"
if thisURL ≠ "NO SITE" then
tell active sheet
make new hyperlink of cell ("K" & r) with properties {address:thisURL, name:thisURL}
end tell
else
tell active sheet
make new cell ("K" & r) with properties {name:"NO SITE"}
end tell
end if
end tell
set r to r + 1
end repeat
Greetings, Michael / Hamburg
Here is another solution. I tested with different delay values inside the System Events part and found a better way of getting the URL from Safari. Have a look at the two main parts of the script, everything else don't need changes:
-- type the search value into the address field and hit return (aka select and open the first proposal)
tell application "System Events"
tell process "Safari"
-- give time to prepare the window
delay 0.5
set focused of text field 1 of group 2 of toolbar 1 of window 1 to true
-- give time to prepare the address field
delay 0.5
keystroke searchTerm
-- give time to get the proposals
delay 0.5
keystroke return
end tell
end tell
-- let Safari open the suggested web page and read out the finally used URL
tell application "Safari"
-- setting the default value
set thisURL to "NO SITE"
try
-- 6 tries with 5 seconds pause (-> max. 30 sec.)
repeat 6 times
try
-- give time to load
delay 5
-- try to access the URL, if it is not available, an error occurs and the repeat loop starts again
set thisURL to URL of document 1
-- close the document
close document 1
-- no error till now, exit the repeat loop
exit repeat
end try
end repeat
on error
-- just to be sure: close all windows
try
close windows
end try
end try
end tell
Greetings, Michael / Hamburg
I've created an Applescript that lets me type in a bunch of commands. I'm working on the section for playing music. Basically, it removes the word play (which is the command) from the string with text delimiters and then searches for the rest of the string in iTunes. If the song is there, it plays it. But if it's not, it just quits. So, I'm wondering if I can display a dialog that says something like "Song not found" if the song isn't in the iTunes library. Here is my code:
display dialog "Enter a command" default answer "" with title "Enter a command"
set userInput to text returned of the result
if userInput contains "Play" then set {TID, text item delimiters} to {text item delimiters, {"Play "}}
set resultString to text item 2 of userInput
set text item delimiters to TID
set playSong to (resultString as string)
if userInput contains "Play" then tell application "iTunes"
set mySongs to every track of library playlist 1 whose name is playSong
repeat with aSong in mySongs
play aSong
end repeat
end tell
Thanks :)
Just count the returned tracks:
if (count of mySongs) = 0 then
display alert "Song not found"
end if
Full script:
display dialog "Enter a command" default answer "" with title "Enter a command"
set userInput to text returned of the result
if userInput contains "Play" then set {TID, text item delimiters} to {text item delimiters, {"Play "}}
set resultString to text item 2 of userInput
set text item delimiters to TID
set playSong to (resultString as string)
if userInput contains "Play" then tell application "iTunes"
set mySongs to every track of library playlist 1 whose name is playSong
repeat with aSong in mySongs
play aSong
end repeat
if (count of mySongs) = 0 then
display alert "Song not found"
end if
end tell
Just a quick question: What's wrong with the following AppleScript code? What it's supposed to do is get the position of a text item (separated by a user-provided delimiter) within a string. But thus far, it doesn't work. Script Debugger simply says, "Can't continue return_string_position" without any specific errors. Any ideas as to what's wrong?
tell application "System Events"
set the_text to "The quick brown fox jumps over the lazy dog"
set word_index to return_string_position("jumps", the_text, " ")
end tell
on return_string_position(this_item, this_str, delims)
set old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set this_list to this_str as list
repeat with i from 1 to the count of this_list
if item i of this_list is equal to this_item then return i
end repeat
set AppleScript's text item delimiters to old_delims
end return_string_position
Your issue is that System Events thinks the function return_string_position is one of its own (if you'll look at the dictionary, you'll find that it isn't). This is very easily resolved; just add my before the call to return_string_position.
Your new code:
tell application "System Events"
set the_text to "The quick brown fox jumps over the lazy dog"
set word_index to my return_string_position("jumps", the_text, " ")
end tell
...
Or you can use adayzdone's solution. In this case, his/her solution is perfect for the job because there really isn't any need to target system events when working with simple textual things.
The tell system events command is incorrect and should be excluded. Also, you don't need to use text item delimiters of " " to make a list of words, simply use "every word of". Lastly, your code will only return the last match of the passed parameter, this will return EACH match.
on return_string_position(this_item, this_str)
set theWords to every word of this_str
set matchedWords to {}
repeat with i from 1 to count of theWords
set aWord to item i of theWords
if item i of theWords = this_item then set end of matchedWords to i
end repeat
return matchedWords
end return_string_position
return_string_position("very", "The coffee was very very very very very ... very hot.")