解析osascript -l JavaScript的命令行参数

时间:2018-12-16 02:22:29

标签: javascript macos applescript

我正在寻找一种获取使用osascript的javascript文件的命令行参数的方法。

我尝试引用可与Apple脚本文件一起使用的argv。 我已经尝试过引用系统,并且已经尝试过引用它。

文档似乎不容易理解……我尝试了多次搜索,但是却发现节点匹配或苹果脚本匹配。

这是我要将参数传递给的脚本。

#!/usr/bin/osascript

console.log(this)

for ( key in this ) {
    console.log(key)
}

var app = Application.currentApplication()
app.includeStandardAdditions = true
var Calendar = Application("Calendar")

var eventStart = app.currentDate()
eventStart = eventStart
eventStart.setDate(eventStart.getDate() + 1)
eventStart.setHours(15)
eventStart.setMinutes(0)
eventStart.setSeconds(0)
var eventEnd = new Date(eventStart.getTime())
eventEnd.setHours(16)

var projectCalendars = Calendar.calendars.whose({name: "csmu"})
var projectCalendar = projectCalendars[0]

var event = Calendar.Event({summary: "Important Meeting!", startDate: eventStart, endDate: eventEnd})
projectCalendar.events.push(event)
event

这是一个示例运行。

./create-event.js '{"calendar":"BD2T-AT"}'
[object GlobalObject]
eventEnd
eventStart
Calendar
event
app
projectCalendar
projectCalendars
Application("Calendar").calendars.whose({_match: [ObjectSpecifier().name, "csmu"]}).calendars.at(0).events.byId("3F6F91B0-06CD-4688-ADED-238BA40915DC")

如何解析osascript -l JavaScript的命令行参数

1 个答案:

答案 0 :(得分:0)

我尝试创建运行函数。可行!

示例脚本。

#!/usr/bin/osascript

function run(command_line_parameters){

    command_line_parameters = JSON.parse(command_line_parameters)

    calendar_name = command_line_parameters['calendar']

    summary = command_line_parameters['summary']

    starts = command_line_parameters['starts']
    start_date_time = starts.split(' ')
    start_date = start_date_time[0]
    start_time = start_date_time[1].split(':')

    ends = command_line_parameters['ends']
    end_date_time = ends.split(' ')
    end_date = end_date_time[0]
    end_time = end_date_time[1].split(':')

    var app = Application.currentApplication()
    app.includeStandardAdditions = true
    var Calendar = Application("Calendar")

    var eventStart = new Date(start_date)

    eventStart.setHours(start_time[0])
    eventStart.setMinutes(start_time[1])
    eventStart.setSeconds(0)

    var eventEnd = new Date(end_date)
    eventEnd.setHours(end_time[0])
    eventEnd.setMinutes(end_time[1])
    eventEnd.setSeconds(0)

    console.log(calendar_name,summary,starts,ends)

    var projectCalendars = Calendar.calendars.whose({name: calendar_name})
    var projectCalendar = projectCalendars[0]

    var event = Calendar.Event({summary: summary, startDate: eventStart, endDate: eventEnd})
    projectCalendar.events.push(event)
    event

}

示例运行。

./create-event.js '{"calendar":"BD2L-AT", "starts":"2018-12-16 09:00", "ends":"2018-12-16 13:43", "summary":"Created from command line"}'

这在BD2L-AT日历中于2018年12月16日09:00至13:43创建了一个事件。