使用 AppleScript 从 .txt 文件读取 time/date 变量
Read time/date variable from .txt file using AppleScript
我正在尝试使用苹果脚本从 .txt
文件中读取时间/日期变量。
根据文件中的时间/日期,我想停止苹果脚本或 运行 苹果脚本的其余部分。
所以 .txt
文件位于我的桌面上,所以我的苹果脚本如下:
set desktop_folder to "$HOME/Desktop"
set myFile to "timeDate.txt"
do shell script "echo my file contents is:" & myFile
if myFile < 2021-09-25 then
error number -128
end if
timeDate.txt
文件中的日期较少 2021-09-25
因此它应该停止 运行ning 中的其余代码。我不明白为什么代码没有停止。
假设 timeDate.txt 的 content 是格式相同的单个条目,例如2021-09-25
没有尾随 linefeed,然后是下面的 example AppleScript code 做你想做的事。
示例 AppleScript 代码:
set myFile to POSIX path of ¬
(path to desktop) & "timeDate.txt"
set dateString to read myFile
if dateString < "2021-09-25" then return
如果您 运行 遇到将 date
与 text/string 进行比较的问题,则使用:
示例 AppleScript 代码:
set thisDate to "2021-09-25"
set myFile to POSIX path of ¬
(path to desktop) & "timeDate.txt"
set dateString to read myFile
if date dateString < date thisDate then return
要针对特定用户比较没有本地化问题的字符串日期,应首先将它们转换为 日期对象:
-- script: example for comparing string dates
-- assuming the content of text file is a single entry,
-- in the same format as e.g. "2021-09-25"
set comparedDateString to "2021-09-25"
set readDateString to read (choose file of type "txt")
set dateObject to (current date)
set dateObject's year to (text 1 thru 4 of comparedDateString) as integer
set dateObject's day to (text 9 thru 10 of comparedDateString) as integer
set dateObject's month to (text 6 thru 7 of comparedDateString) as integer
set comparedDate to dateObject
set dateObject's year to (text 1 thru 4 of readDateString) as integer
set dateObject's day to (text 9 thru 10 of readDateString) as integer
set dateObject's month to (text 6 thru 7 of readDateString) as integer
set readDate to dateObject
if readDate < comparedDate then error number -128
我正在尝试使用苹果脚本从 .txt
文件中读取时间/日期变量。
根据文件中的时间/日期,我想停止苹果脚本或 运行 苹果脚本的其余部分。
所以 .txt
文件位于我的桌面上,所以我的苹果脚本如下:
set desktop_folder to "$HOME/Desktop"
set myFile to "timeDate.txt"
do shell script "echo my file contents is:" & myFile
if myFile < 2021-09-25 then
error number -128
end if
timeDate.txt
文件中的日期较少 2021-09-25
因此它应该停止 运行ning 中的其余代码。我不明白为什么代码没有停止。
假设 timeDate.txt 的 content 是格式相同的单个条目,例如2021-09-25
没有尾随 linefeed,然后是下面的 example AppleScript code 做你想做的事。
示例 AppleScript 代码:
set myFile to POSIX path of ¬
(path to desktop) & "timeDate.txt"
set dateString to read myFile
if dateString < "2021-09-25" then return
如果您 运行 遇到将 date
与 text/string 进行比较的问题,则使用:
示例 AppleScript 代码:
set thisDate to "2021-09-25"
set myFile to POSIX path of ¬
(path to desktop) & "timeDate.txt"
set dateString to read myFile
if date dateString < date thisDate then return
要针对特定用户比较没有本地化问题的字符串日期,应首先将它们转换为 日期对象:
-- script: example for comparing string dates
-- assuming the content of text file is a single entry,
-- in the same format as e.g. "2021-09-25"
set comparedDateString to "2021-09-25"
set readDateString to read (choose file of type "txt")
set dateObject to (current date)
set dateObject's year to (text 1 thru 4 of comparedDateString) as integer
set dateObject's day to (text 9 thru 10 of comparedDateString) as integer
set dateObject's month to (text 6 thru 7 of comparedDateString) as integer
set comparedDate to dateObject
set dateObject's year to (text 1 thru 4 of readDateString) as integer
set dateObject's day to (text 9 thru 10 of readDateString) as integer
set dateObject's month to (text 6 thru 7 of readDateString) as integer
set readDate to dateObject
if readDate < comparedDate then error number -128