使用以下AppleScript代码,实现在终端中运行脚本时,文件只在第一次运行时进行更改,而不是每次运行都更改。
property filePath : "path/to/file.txt" --更改为实际文件路径
set fileChanged to read file filePath
if fileChanged is not "changed" then
set fileId to open for access filePath with write permission
set eof fileId to 0
write "changed" to fileId
close access fileId
-- 运行需要更改的脚本代码
end if
代码中首先定义了 filePath
变量,表示需要更改的文件的路径。然后读取该文件的内容,即 fileChanged
。如果该内容不是 "changed"
,则通过打开文件并将内容设置为 "changed"
来更改了文件,并继续运行需要更改的代码。
这样,在终端中再次运行脚本时,会读取到已更改的文件,并且不会再次更改该文件。