苹果脚本倒计时



我想要一个从当前时间倒计时到闹钟时间的字符串。

我已经设法弄清楚如何获取当前时间以及如何设置闹钟时间。

我遇到的问题是,当我获取当前时间 - 闹钟时间时,它给了我一个数字女巫,然后我需要格式化回 hh:mm:ss 字符串。

我有这个。

set alarmHour to 23
set alarmMinute to 00
set theDate to the current date
set the hours of theDate to alarmHour
set the minutes of theDate to alarmMinute
set the seconds of theDate to 0
theDate
set countdown to theDate - (current date)
set ss to countdown / 60

在这一点上,它给了我 22.283333333333 女巫我现在需要转换为 HH:MM:SS,然后将它们放入一个给我 00:22:00 的刺

更新:在 Swift 中,您可以使用 %

 countDownTime = (formatterInteger - timeControlInteger)
    let interval    = Int(countDownTime)
    let seconds     = interval % 60
    let minutes     = (interval / 60) % 60
    let hours       = (interval / 3600)

但是如何在Applescript中执行此操作?

回答第二个问题:

有没有办法像在 swift 中那样格式化字符串? String(format:"%02d",absHour) – Mathias Halén

是的,但您需要使用 Satimage.osax 脚本添加功能,可在以下网址免费获得:Satimage AppleScript 新增内容

Satimage strftime() -- 日期/时间格式函数

strftime v :使用规范字符串格式化日期,如 C 中的格式 函数 strftime。

时间日期或日期列表

成字符串:格式化字符串。要获取 ISO 8601 日期,请使用 "%FT%TZ" 或 "%GW%V-%uT%TZ" (使用 'with GMT' 参数)

[GMT 布尔值] :如果为 true,则输出日期为 GMT。默认值:假,输出 日期为本地日期。

→字符串:格式化的日期

示例:strftime(当前日期)转换为"%x" 返回:07/22/14

"%a, %b %d, %Y" RETURNS: Tue, Jul 22, 2014 set d to current date -- some ISO 8601 formats: strftime d into "%FT%T%z" -- "2007-01-15T16:10:56+0100" strftime d into "%GW%V-%uT%T%z" -- "2007W03-1T16:10:56+0100" --if you need to store the date d as UTC: strftime d into "%FT%TZ" with GMT -- "2007-01-15T15:10:56Z" strftime d into "%a, %b %d, %Y %H:%M:%S %z" -- "Mon, Jan 15, 2007 16:10:56 +0100"

最新更新