-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdatepicker.osa
85 lines (77 loc) · 3.62 KB
/
datepicker.osa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 06/04/16 09:35:02
# Author: Shane Stanley
# Adapted by Christopher Stone
#use AppleScript version "2.3.1"
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
my datePicker()
on datePicker()
set theApp to path to frontmost application as text
if not (current application's NSThread's isMainThread()) as boolean then
display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
error number -128
end if
-- create a view
set theView to current application's NSView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, 100, 175))
-- create date picker
set datePicker to current application's NSDatePicker's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, 100, 100))
-- set style: choices are NSTextFieldAndStepperDatePickerStyle, NSClockAndCalendarDatePickerStyle, or NSTextFieldDatePickerStyle
#datePicker's setDatePickerStyle:(current application's NSClockAndCalendarDatePickerStyle)
datePicker's setDatePickerStyle:(current application's NSTextFieldAndStepperDatePickerStyle)
#datePicker's setDatePickerStyle:(current application's NSTextFieldDatePickerStyle)
-- set elements: choices include NSHourMinuteDatePickerElementFlag, NSHourMinuteSecondDatePickerElementFlag, NSTimeZoneDatePickerElementFlag, NSYearMonthDatePickerElementFlag, and NSEraDatePickerElementFlag
datePicker's setDatePickerElements:((current application's NSYearMonthDayDatePickerElementFlag) + (current application's NSHourMinuteSecondDatePickerElementFlag as integer))
-- set initial date
#datePicker's setDateValue:(current application's NSDate's |date|())
--set theCal var to a new(empty) instance of the calendar
set theCal to current application's class "NSCalendar"'s currentCalendar()
-- unpack the components of theCal to a variable
set theComponents to theCal's components:254 fromDate:(current application's NSDate's |date|())
theComponents's setSecond:0
theComponents's setMinute:0
theComponents's setHour:12
theComponents's setYear:2015
theComponents's setMonth:4
theComponents's setDay:1
datePicker's setDateValue:(theCal's dateFromComponents:theComponents)
-- get the size it needs
set theSize to datePicker's fittingSize()
--resize the picker and view accordingly
theView's setFrameSize:theSize
datePicker's setFrameSize:theSize
-- add the picker to the view
theView's setSubviews:{datePicker}
-- create an alert
set theAlert to current application's NSAlert's alloc()'s init()
-- set up alert
tell theAlert
its setMessageText:"Pick a date and time"
its setInformativeText:"Any date"
its addButtonWithTitle:"OK"
its addButtonWithTitle:"Cancel"
its setAccessoryView:theView
end tell
-- show alert in modal loop
set returnCode to theAlert's runModal()
if returnCode = (current application's NSAlertSecondButtonReturn) then error number -128
-- retrieve date
#set theDate to datePicker's dateValue() as date
# Shane Stanley
# http://macscripter.net/viewtopic.php?pid=119633#p119633
set theDate to datePicker's dateValue()
#set theCal to current application's class "NSCalendar"'s currentCalendar()
-- unpack the components of theDate to a variable
set theComponents to theCal's components:254 fromDate:theDate
set theDate to current date
set day of theDate to 1 -- important
set seconds of theDate to theComponents's |second|()
set year of theDate to theComponents's |year|()
set month of theDate to theComponents's |month|()
set day of theDate to theComponents's |day|()
set hours of theDate to theComponents's hour()
set minutes of theDate to theComponents's minute()
return (theDate as text)
#return (theDate)
end datePicker