How to check internet connection applescript - applescript

noob here trying to teach myself applescript and generally not very smart so apologies :|
i was looking at this question and the answers
Check for active internet connection with Applescript/Automator
and i wanted to make a applescript that runs constantly in the background and that puts a red dot in the menubar when i don't have an internet connection and a green one when i don't (using the AnyBar application) but i can't get it to work properly.
can someone please tell me what i am doing wrong. thank you so much!
repeat
repeat with i from 1 to 2
try
do shell script "ping -o -t 2 www.google.com"
exit repeat
tell application "AnyBar" to set image name to "green"
on error
tell application "AnyBar" to set image name to "orange"
delay 2
if i = 2 then tell application "AnyBar" to set image name to "red"
end try
end repeat
delay 60
end repeat

I'd recommend to use an applet (script saved as application) with an idle handler rather than an infinite repeat loop
property imageName : "red"
property delayValue : 60
property googleURL : "http://www.google.com"
on run
set imageName to "red"
end run
on idle
if (count (get ((googleURL as URL)'s host & {dotted decimal form:""})'s dotted decimal form)) > 0 then
set imageName to "green"
else
if imageName is "green" then
set imageName to "orange"
set delayValue to 2
else if imageName is "orange" then
set imageName to "red"
set delayValue to 60
end if
end if
tell application "AnyBar" to set image name to imageName
return delayValue
end idle

Related

Applescript - ghosted result with applescript set file label

I did make this script in automator.
property unset : 0
property orange : 1
property red : 2
property yellow : 3
property blue : 4
property purple : 5
property green : 6
property grey : 7
property tmplFileName : "__ReadMe" as string
property fileType : ".txt" as string
property labelTag : red
on run {input, parameters}
tell application "Finder"
set currentPath to insertion location as text
-- set filePath to POSIX path of currentPath
set txtfilename to tmplFileName & fileType
set txtFile to make new file at insertion location as alias with properties {name:txtfilename}
set the label index of the item (txtFile as alias) to my labelTag
select txtFile
open txtFile
end tell
return input
end run
It give's me the following result. A ghosted or visible only disabled file.
What do I wrong? This happens only when I set the label. Its about this line
set the label index of the item (txtFile as alias) to my labelTag
Any suggestions? Thank you.
Still don't understand why the file is disabled. Anyway and moving forward, after some reading I came with this as a test script, and it works. Need some error management like check existing file.
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
property |NSURL| : a reference to current application's NSURL
property tmplFileName : "__ReadMe" as string
property fileType : ".txt" as string
property labelTag : "red"
-- Replace tags;
-- setTags - pass a list with new tags and replacing any existing
-- forPath - POSIX path
on setTags:tagList forPath:POSIXPath
set aURL to current application's |NSURL|'s fileURLWithPath:POSIXPath
aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end setTags:forPath:
on run {input, parameters}
tell application "Finder"
set txtfilename to tmplFileName & fileType
set txtFile to make new file at insertion location as alias with properties {name:txtfilename}
set POSIXPath to POSIX path of (txtFile as text)
(my setTags:{labelTag} forPath:POSIXPath)
select txtFile
tell application "TextEdit"
activate
open txtFile as alias
end tell
end tell
return input
end run
Hope this help some other people as well. Do I get now a plus one vote back from above. As a side note, can some one explain why I did get a -1? just curious.
Thank you.
I took a stab in the dark and thought maybe the problem was the size of the file that was created, was 0 kb's. I decided to add a handler with the write to file command and added a single space as a character to the file. This made the file size 1 byte and then setting the label index worked. I made a few minor adjustments to your initial code, for my purposes but here are the changes I've made and my version of the code.
local unset, orange, red, yellow, blue, purple, green, grey
set {unset, orange, red, yellow, blue, purple, green, grey} to {0, 1, 2, 3, 4, 5, 6, 7}
set tmplFileName to "__ReadMe"
set nameExtension to ".txt"
set labelTag to red
tell application "Finder"
set currentPath to insertion location
set txtfilename to tmplFileName & nameExtension
set txtFile to make new file at insertion location as alias with properties {name:txtfilename, name extension:nameExtension}
my writeToTheFile(txtFile as alias)
set the label index of (txtFile as alias) to labelTag
open txtFile
end tell
on writeToTheFile(txtFile as alias)
set theFile to txtFile as alias
set theText to " "
try
set writeToFile to open for access theFile with write permission
write theText to writeToFile as text starting at eof
close access theFile
on error errMsg number errNum
try
close access theFile
end try
end try
end writeToTheFile

Applescript one window resizes, the other doesn't

I'm getting an unusual result when resizing windows via applescript.
I'm trying to set the position and size of Excel's windows but in one window's case it works and in another it doesn't.
Here's the code:
tell application "System Events"
get the name of every application process whose class of windows contains window
repeat with P in the result
get (every window of process (contents of P) whose value of attribute "AXMinimized" is false)
repeat with W in the result
set win_name to name of W
log P & ":" & win_name
if (win_name is equal to "VBA-Web - Blank") then
log "CASE1"
tell W
set {position, size} to {{0, 0}, {1100, 500}}
end tell
end if
if (win_name is equal to "Microsoft Visual Basic - VBA-Web - Blank.xlsm - [ThisWorkbook (Code)]") then
log "CASE2"
tell W
set {position, size} to {{0, 0}, {1120, 520}}
end tell
end if
end repeat
end repeat
end tell
In the code above, CASE1 works fine, but CASE2 does nothing.
I suspect the VBA editor window is somehow different... but how?
Any suggestions on what I can try to fix this?

Set a camera in quicktime using applescript

I know there is probably a very simple answer to this. I'm trying to setup an Applescript application that when activated will launch quicktime, open a new movie recording, set the camera and audio, start the recording and then end the recording and save it after 30 seconds.
I currently have a script that is doing everything except setting the camera and audio source. Any tips on how to select a certain camera and audio source using applescript?
Thanks!
Here is the code as it stands now..
--Set some Variables
--Max length of recording
set maxrec to 20 --in seconds
--Ending Message
set EndMsg to "Thank you for participating in this project. Your video has been recorded."
--Begin Loop
repeat
--Get the person's name
repeat
display dialog "What's your name?" default answer ""
set theName to (text returned of result)
if theName ≠ "" then exit repeat
end repeat
--Set the date
set theDate to current date
set y to text -4 thru -1 of ("0000" & (year of theDate))
set m to text -2 thru -1 of ("00" & ((month of theDate) as integer))
set d to text -2 thru -1 of ("00" & (day of theDate))
set h to text -2 thru -1 of ("00" & (hours of theDate))
set mm to text -2 thru -1 of ("00" & (minutes of theDate))
set dateStamp to (y & "-" & m & "-" & d & " " & h & mm as string)
--Create a folder for the person
tell application "Finder"
set p to path to movies folder from user domain
try
make new folder at p with properties {name:"Video Clips"}
end try
set p to (path to movies folder from user domain as text) & "Video Clips:"
make new folder at p with properties {name:dateStamp & " " & theName}
set thePath to result
--Establish the final name of the movie
set fileName to ((thePath as text) & dateStamp & ".mov" as string)
end tell
--Open New Recording, start and stop it
tell application "QuickTime Player"
set newMovieRecording to new movie recording
set windowID to id of first window whose name = "Movie Recording"
delay 2
tell newMovieRecording
--Set Camera to Blackmagic
set current camera of newMovieRecording to Blackmagic of video recording devices
start
end tell
set theSeconds to maxrec
repeat theSeconds times
display dialog theSeconds buttons {} giving up after 1 with title "REMAINING TIME"
set theSeconds to (theSeconds - 1)
end repeat
tell newMovieRecording
stop
end tell
--save and close the recording
set newMovieRecordingDoc to first document whose name = (get name of first window whose id = windowID)
tell newMovieRecordingDoc to save in fileName
set SavedRecordingDoc to first document whose name = (get name of first window whose id = windowID)
tell SavedRecordingDoc to close
quit
end tell
--Display "Thank You" Message
tell application "System Events" to set frontmost of process "Video Booth" to true
display dialog EndMsg giving up after 20 buttons {"Continue…"} ¬
default button 1
end repeat
Here is the error I get right now when I run it.
error "QuickTime Player got an error: Can’t make Blackmagic of every video recording device of document \"Movie Recording\" into type specifier." number -1700 from Blackmagic of every video recording device of document "Movie Recording" to specifier
use the current camera and current microphone properties of your movie recording object. Just replace item 1 with the appropriate device:
set current camera of recording to item 1 of video recording devices
set current microphone of recording to item 1 of audio recording devices
I had accomplished this by doing it manually
tell application "System Events" to tell process "QuickTime Player"
#To open dialog to show available cameras
click button 3 of window 1
#To select our device
click menu item "your_camera_name" of menu 1 of button 3 of window 1
end tell
With this the dialog will open and you will select your camera from the list...
Taken from another answer I made:
https://stackoverflow.com/a/45454785/3685973

How does one convert a QuickTime duration in frames to a time format using AppleScript?

I'm relatively new to AppleScript. I'm trying to convert a QuickTime duration to a mm:ss time format that rounds to the nearest second. Currently, it comes up as minutes and decimal places that I want to convert to seconds.
I much appreciate any suggestions.
Here's what I have, so far…
set these_items to choose file with prompt "Select source file(s)" with multiple selections allowed
repeat with oneItem in these_items
tell application "System Events"
try
set q to QuickTime file (oneItem as text)
set MovieDuration to ((duration of contents of q) / (time scale of contents of q)) / 60
display dialog "Movie duration of " & name of q & " = " & MovieDuration
end try
end tell
end repeat
I think I've figured it out. This seems to give me the result I want, anyway:
set these_items to choose file with prompt "Select source file(s)" with multiple selections allowed
repeat with oneItem in these_items
tell application "System Events"
try
set q to QuickTime file (oneItem as text)
set MovieSeconds to (((duration of contents of q) / (time scale of contents of q)) mod 60) as string
set x to text 1 thru ((offset of "." in MovieSeconds) - 1) of MovieSeconds
set MovieSeconds to text -2 thru -1 of ("00" & x)
set MovieMinutes to ((duration of contents of q) / (time scale of contents of q)) div 60
display dialog "Movie duration of " & name of q & " = " & MovieMinutes & ":" & MovieSeconds
end try
end tell
end repeat
Another way to get the timestamp duration is to give the movie a date with a midnight time and then add the duration seconds to it, and then ask for the “time string” of that date. The time string is formatted HH:MM:SS.
This example works with QuickTime Player 10.3 on Mavericks:
tell application "QuickTime Player"
activate
open (choose file with prompt "Choose a movie file:")
set theMovie to a reference to the front document
set theMovieDurationSeconds to the duration of theMovie
set theMovieDate to date "Saturday, January 1, 2000 at 00:00:00"
set theMovieDate to theMovieDate + theMovieDurationSeconds
set theMovieDurationTimeString to the time string of theMovieDate
display dialog "The movie duration:" & space & theMovieDurationTimeString buttons {"Cancel", "OK"} default button "OK" with title (the name as text) with icon note giving up after 60
end tell
The time string will be “00:08:20” for a movie with a duration of 500 seconds.
The advantage of this method is you don’t have to do any math, and it should work with movies that are up to 23:59:59 in duration.
Also notice that you can just ask QuickTime Player for the movie duration.

Check for multiple screen resolutions

How would I modify this script to look for images for multiple screen resolutions? Right now it checks for a single screen resolution (640x1136), but I would like to also check for 640x960 and 1024x768.
on run {input, parameters}
set picFolder to alias "Users:colind:Dropbox:Camera Uploads:"
set screenshotFolder to alias "Users:colind:Dropbox:Camera Uploads:Screenshots:"
tell application "System Events"
set photos to path of files of picFolder whose kind is "Portable Network Graphics image"
end tell
set screenshots to {}
repeat with imgPath in photos
set imgAlias to alias imgPath
tell application "Image Events"
set img to open imgPath
if dimensions of img = {640, 1136} then
set end of screenshots to imgAlias
end if
close img
end tell
end repeat
tell application "Finder"
move screenshots to screenshotFolder
end tell
return input
end run
You should be able to change the if line to:
if ((dimensions of img = {640, 1136}) or (dimensions of img = {640, 960}) or (dimensions of img = {1024, 768}) ) then

Resources