【问题标题】:Add a number to variable applescript将数字添加到变量applescript
【发布时间】:2013-05-31 00:25:45
【问题描述】:

我有一个写在文本文件中的数字。是否可以在文本文件中添加另一个号码,然后用新号码替换旧号码?这是我的代码:

set theFile to POSIX path of "Users:Tyler:Documents:File.txt"
open for access theFile
set theFileContents to read theFile

set theNewFile to run script theFileContents + 1
tell application "TextEdit"
set eof of theFileContents to 0
write theNewFile to theFileContents

【问题讨论】:

    标签: variables text numbers applescript add


    【解决方案1】:

    如果我正确理解了您的问题,您只想拥有一个带有字符串的文本文件并将该字符串转换为数字,然后将数字的值增加 1 将其作为字符串写回文件并保存完成对 ?让我知道这是否是您的意思。

    set docsFolder to path to documents folder
    
    set afile to "" & docsFolder & "File.txt"
    open for access file afile with write permission
    set oldNum to read file afile
    set newString to ((oldNum as number) + 1) as string
    set eof file afile to 0
    log newString
    write newString to file afile starting at eof
    
    close access file afile
    

    【讨论】:

    • 这正是我想要做的,而且效果很好。谢谢!
    • 不客气。不过我很好奇,你想用运行脚本做什么?
    • @mcgralim 我以前用过运行脚本来制作计算器。 set theMath to text returned of (display dialog "Insert math problem here" default answer "" buttons {"Cancel", "Ok"} default button 2)运行脚本theMath
    • 嗯好吧,我只是在你的帖子中没有看到任何理由我不明白你期望它做什么
    猜你喜欢
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多