Is event in specified date in Apple calendar by AppleScript? - events

I need to test if in specified day has any events in the built-in Apple Calendar app. Here is my current idea as pseudocode:
// input variables
day_to_test = 23
month_to_test = 8
year_to_test = 2016
testing_date = day_to_test + month_to_test + year_to_test as date
// actual "code"
if testing_date has any event in Apple calendar Then
display dialog "There is any event on that day!"
else
display dialog "Free of events!"
end if

Try:
tell application "Calendar"
tell calendar "Main" -- national holiday calendar in my iCloud
set today to current date
set today's year to 2013
set today's month to 3
set today's day to 5
set today's time to 0
copy (today + days) to tomorrow
set currentEvents to events whose start date ≥ today and end date ≤ tomorrow
end tell
end tell

Related

AppleScript Progress Bar with Reboot Button

I've recently been tasked with working on a project to switch antivirus software in the environment as we are changing to a new vendor. Naturally, this kind of effort will require a reboot on our endpoints once the old A/V software has been removed.
What I'm trying to do is create a reboot notification prompt for our Mac customers (since many are high up on the food chain) to let them know what we are doing.
This prompt will let them know they have 3 hours to reboot their Macs to complete the removal of the software if they are logged in. If they do not reboot within 3 hours, their workstation will automatically reboot.
Applescript has a great workflow that allows me to effectively communicate with our users, but I would like to change the Stop button to a reboot, so that our customers can reboot their Macs on demand.
I will work in logic later on to automatically reboot if they are not logged in.
I modified one of the scripts that I, "borrowed," from here
https://macscripter.net/viewtopic.php?id=46572
-- Progress Bar - Reboot Timer
progress_timer("03:00:00", "Reboot Timer") -- call the progress timer with an HMS time and timer label
return result
------------------------------------------
-- subroutines in alphabetical order --
------------------------------------------
-- getTimeConversion converts a time in HMS format (hh:mm:ss) to a time in seconds
on getTimeConversion(HMS_Time)
set HMSlist to the words of HMS_Time -- get {hh, mm, ss} from HMS time (p. 158 Rosenthal)
set theHours to item 1 of HMSlist
set theMinutes to item 2 of HMSlist
set theSeconds to item 3 of HMSlist
return round (theHours * (60 ^ 2) + theMinutes * 60 + theSeconds)
end getTimeConversion
-- progress_timer displays the elapsed time in a progress bar. For information on progress bars see: https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/DisplayProgress.html
on progress_timer(HMS_Time, timerLabel)
set theTimeSec to getTimeConversion(HMS_Time) -- convert the HMS format to seconds
set progress total steps to theTimeSec
set progress completed steps to 0
set startTime to (current date)
repeat with i from 0 to theTimeSec -- begin at i = 0 to start the timer at 00:00:00
set HMS_SoFar to TimetoText(i) -- convert the seconds so far to HMS format for display
set HMS_ToGo to TimetoText(theTimeSec - i) -- convert the seconds to go to HMS format for display
set progress completed steps to 0
set progress description to "
Your IT Department needs to make changes to your Mac.
Your workstation must be rebooted in order for these changes to take effect.
Your workstation will reboot in " & HMS_ToGo
set progress additional description to ¬
""
--"Time Elapsed: " & HMS_SoFar & return & ¬
--"Counting Down: " & HMS_ToGo
set progress completed steps to i
set elapsedTime to (current date) - startTime -- get actual elapsed time for adjusting delay
set lagAdjust to elapsedTime - i -- compute lag adjustment
delay 1 - lagAdjust -- delay 1 second minus any cumulative lag that needs removing
end repeat
--set HMS_Elapsed to TimetoText(elapsedTime) -- convert elapsedTime back to HMS format for display
set dialogText to null
--set dialogText to "Elapsed Time: " & return & ¬
-- "Nominal = " & HMS_Time & return & ¬
-- "Actual = " & HMS_Elapsed
tell me to activate
--display dialog dialogText with title timerLabel & " Timer"
return dialogText
end progress_timer
-- TimetoText converts a time in seconds to a time in HMS format (hh:mm:ss)
on TimetoText(theTime)
-- parameters - TheTime [integer]: the time in seconds
-- returns [text]: the time in the format hh:mm:ss
-- Nigel Garvey points out this script is only valid for parameter values up to 86399 seconds (23:59:59) and offers a solution for longer times here: https://macscripter.net/viewtopic.php?pid=134656#p134656
if (class of theTime) as text is "integer" then
set TimeString to 1000000 + 10000 * (theTime mod days div hours) -- hours
set TimeString to TimeString + 100 * (theTime mod hours div minutes) -- minutes
set TimeString to (TimeString + (theTime mod minutes)) as text -- seconds
tell TimeString to set theTime to (text -6 thru -5) & ":" & (text -4 thru -3) & ":" & (text -2 thru -1)
end if
return theTime
end TimetoText
Here's a screenshot of the resulting output.

trying to write an apple script that chimes every hour

I'm trying to write this script that chimes on every hour. I saved it as an application and selected the checkbox run after completion but it doesn't work. My code looks like this:
global chime
set chime to (path to resource "chime.mp3")
on idle
set currenthour to hours of (current date)
if currenthour = 0 then
set currenthour to 12
end if
if currenthour > 12 then
set currenthour to currenthour - 12
end if
set currentminute to minutes of (current date)
set currentsecond to seconds of (current date)
set currenttime to {currentminute, currentsecond} as text
if currenttime is "" then
repeat currenthour times
do shell script "afplay " & (quoted form of POSIX path of chime)
end repeat
end if
return 1
end idle
In a script app, the number you return from the on idle handler tells the system how long to sleep the app before the next idle invocation. You can use this set up a (loosely) accurate timer.
global chime, firstRun
on run
-- I'm not sure if this is necessary, but I always use explicit run handlers in script apps.
set chime to (path to resource "chime.mp3")
set firstRun to true
end run
on idle
set {currenthour, currentminute, currentsecond} to {hours, minutes, seconds} of (current date)
-- don't chime when the script app is activated
if not firstRun then
-- quick mathy way to retrieve the number of chimes.
set chimeCount to (currenthour + 11) mod 12 + 1
repeat chimeCount times
do shell script "afplay " & (quoted form of POSIX path of chime)
end repeat
else
set firstRun to true
end if
-- calculate the number of seconds until the next hour mark and tell app to sleep until then
return 60 * (60 - currentminute) + 60 - currentsecond
end idle

Days from dates in qlikview expression

I am trying to get days from given data like this:
In this data suppose ID B start date is 4/10/2019 and end date is 10/25/2019. Here there is 7 months: April to October, so for the first month start date is 4/10/2019 and end date is 4/30/2019 so this means he only avail 10 days from this month and remaining days is 21.. same for here end date is 10/25/2019 so if we look calendar end date 10/31/2019 we only avail 6 days so in data I want to get above data which is mentioned in image .. where as I am trying this formula in qlikview:
=sum(
If(
MonthName(CalendarMonthEnd) = MonthName([End Date]),
([End Date]-CalendarMonthStart+1),
(RangeMin([End Date],CalendarMonthEnd)-RangeMax([Start Date],CalendarMonthStart))
)
)
and through this formula I get this data which is remaining days where i want to get days which is availed...
this is link of folder please download and check ..
https://www.dropbox.com/s/v48373io1bv9qqj/file_qlik.rar?dl=0
in this folder the excel file "output.. " in this excel file the first table output which i need
Just add another if
=Sum(
If(CalendarMonthStart >= [Start Date] and CalendarMonthEnd <= [End Date],
CalendarMonthEnd-CalendarMonthStart,
If([Start Date]>CalendarMonthStart,
[Start Date]-CalendarMonthStart+1,
CalendarMonthEnd-[End Date])
)
)
)

Applescript: Getting week number from file name

I have lots of pics from my smartphone. The file name format for all of them is yyyy-mm-dd hh:mm:ss.jpg
I need to sort them into the folders according to the week number.
I have no problems moving files to folders with AppleScript :) I also have no problems defining a week number for a given date. I have a problem (as I think) with syntax or even data types but I cannot understand where the problem lies.
Here's what I'm using.
I have a function for defining a week number for a given date
I scan through my folder and get all the names into the list
I parse the names to get yyyy, mm and dd as variables
Then I try to call for my function with these variables - and here's where the problem starts.
Please take a look at this example of my code:
property DBFolder : "Macintosh HD:Users:lyubovberezina:Dropbox:DB"
property CUFolder : "Macintosh HD:Users:lyubovberezina:Dropbox:Camera Uploads"
on weekNumber(_inputDate)
script Week_Number_Extras
on dateOfFirstWeekOfYear(_year)
-- Get Monday of first week number
set _date to current date
set year of _date to _year
set month of _date to 1
set day of _date to 1
set time of _date to 0
-- Get the first Thursday of this year
repeat until weekday of _date is Thursday
set _date to _date + (24 * 60 * 60)
end repeat
-- Return the Monday before
set _date to _date - (3 * 24 * 60 * 60)
return _date
end dateOfFirstWeekOfYear
end script
-- Make a copy of the passed date object to avoid changes to the original
copy _inputDate to _targetDate
-- Reset the time and go back to Monday
set time of _targetDate to 0
repeat until weekday of _targetDate is Monday
set _targetDate to _targetDate - (24 * 60 * 60)
end repeat
-- Get the date of the first week for the current year and the next one
tell Week_Number_Extras
set _matchDate to dateOfFirstWeekOfYear(year of _targetDate)
set _nextYearsFirstWeekDate to dateOfFirstWeekOfYear((year of _targetDate) + 1)
end tell
-- Exit early, if the current week is the first one of next year
if _targetDate = _nextYearsFirstWeekDate then return 1
-- Count up until the target date is reached
set _weekNumber to 1
repeat until _targetDate = _matchDate
set _matchDate to _matchDate + (7 * 24 * 60 * 60)
set _weekNumber to _weekNumber + 1
end repeat
return _weekNumber
end weekNumber
tell application "Finder"
set this_folder to folder CUFolder
set this_list to every file of this_folder
repeat with i in this_list
set fileName to name of i
set fileYear to characters 1 thru 4 of fileName as string
set fileMonth to characters 6 thru 7 of fileName as string
set fileDay to characters 9 thru 10 of fileName as string
end repeat
end tell
set theNewDate to date (fileMonth & "/" & fileDay & "/" & fileYear)
weekNumber(theNewDate)
In this case the line
set theNewDate1 to date (fileMonth & "/" & fileDay & "/" & fileYear)
is working ok (I mean, no errors) but as it's outside the loop it gives the result for the last image in the loop only.
But if I move it to the loop then it gives an error:
property DBFolder : "Macintosh HD:Users:lyubovberezina:Dropbox:DB"
property CUFolder : "Macintosh HD:Users:lyubovberezina:Dropbox:Camera Uploads"
on weekNumber(_inputDate)
script Week_Number_Extras
on dateOfFirstWeekOfYear(_year)
-- Get Monday of first week number
set _date to current date
set year of _date to _year
set month of _date to 1
set day of _date to 1
set time of _date to 0
-- Get the first Thursday of this year
repeat until weekday of _date is Thursday
set _date to _date + (24 * 60 * 60)
end repeat
-- Return the Monday before
set _date to _date - (3 * 24 * 60 * 60)
return _date
end dateOfFirstWeekOfYear
end script
-- Make a copy of the passed date object to avoid changes to the original
copy _inputDate to _targetDate
-- Reset the time and go back to Monday
set time of _targetDate to 0
repeat until weekday of _targetDate is Monday
set _targetDate to _targetDate - (24 * 60 * 60)
end repeat
-- Get the date of the first week for the current year and the next one
tell Week_Number_Extras
set _matchDate to dateOfFirstWeekOfYear(year of _targetDate)
set _nextYearsFirstWeekDate to dateOfFirstWeekOfYear((year of _targetDate) + 1)
end tell
-- Exit early, if the current week is the first one of next year
if _targetDate = _nextYearsFirstWeekDate then return 1
-- Count up until the target date is reached
set _weekNumber to 1
repeat until _targetDate = _matchDate
set _matchDate to _matchDate + (7 * 24 * 60 * 60)
set _weekNumber to _weekNumber + 1
end repeat
return _weekNumber
end weekNumber
tell application "Finder"
set this_folder to folder CUFolder
set this_list to every file of this_folder
repeat with i in this_list
set fileName to name of i
set fileYear to characters 1 thru 4 of fileName as string
set fileMonth to characters 6 thru 7 of fileName as string
set fileDay to characters 9 thru 10 of fileName as string
set theNewDate to date (fileMonth & "/" & fileDay & "/" & fileYear)
weekNumber(theNewDate)
end repeat
end tell
The error is given fight after the first file is taken:
get name of document file "2013-01-02 02.43.21.jpg" of folder "Camera Uploads" of folder "Dropbox" of folder "lyubovberezina" of folder "Users" of startup disk
--> "2013-01-02 02.43.21.jpg"
get date "01/02/2013"
--> error number -1728 from date "01/02/2013"
Result:
error "Finder got an error: Can’t get date \"01/02/2013\"." number -1728 from date "Wednesday, January 2, 2013 12:00:00 AM"
I am an almost total newbie in Applescript so I cannot understand why the code is working outside the loop and not working inside the loop. I would appreciate any help with this matter.
Thanks a lot!
It might be easier to use a shell script at least for the week number part:
do shell script "date -jf %F 2013-07-19 +%V"
cd ~/Dropbox/Camera\ Uploads; for f in *; do d=../DB/$(date -jf %F ${f:0:10} +%Y-%V "$f"); mkdir -p $d; mv "$f" $d; done
You should stick to your original format of:
set theNewDate to current date
set theNewDate's year to fileYear
set theNewDate's month to fileMonth
set theNewDate's day to fileDay
Also:
set _date to _date + (24 * 60 * 60)
is the same as
set _date to _date + days
set _date to _date - (3 * 24 * 60 * 60)
is the same as
set _date to _date - (3 * days)
set _matchDate to _matchDate + (7 * 24 * 60 * 60)
is the same as
set _matchDate to _matchDate + weeks

Working with Rails Timezones?

Working on a 3 week new registration chart for metrics, I have the following code:
(3.weeks.ago.to_date..Date.today).map { |date| Metrics.registrations_on(date) }
In Metrics.rb:
def self.registrations_on(date)
date = date.midnight
end_date = date + 24.hours
User.where(:created_at => date..end_date).count
end
Before the day is done here in California, a new day's numbers are already starting to increase. The created_at timestamp is UTC as well.
I'd like to be able to see the stats from today, using our time zone. With my data already saved as UTC I'm curious as to how about accomplishing this.
Open config/application.rb, find config.time_zone, and assign it with appropriate value:
config.time_zone = 'Pacific Time (US & Canada)'
Restart your app, and all ruby date/time operation should be adjusted automatically to your time zone.
For a list of all supported time zone strings, use:
bundle exec rake time:zones:all

Resources