代码之家  ›  专栏  ›  技术社区  ›  Keith John Hutchison

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

  •  0
  • Keith John Hutchison  · 技术社区  · 6 年前

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

    我试过引用系统,也试过引用这个。

    文件似乎在地面上很轻。。。我尝试了多个搜索,但我要么得到节点匹配,要么得到apple脚本匹配。

    这就是我想要传递参数的脚本。

    #!/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-Ljavascript的命令行参数

    1 回复  |  直到 6 年前
        1
  •  2
  •   Keith John Hutchison    6 年前

    我尝试创建一个run函数。成功了!

    示例脚本。

    #!/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"}'