text - Applescript,将行读取为变量

标签 text applescript

我希望 Applescript 从 txt 文件设置变量,逐行或逐字符读取。也许像 php 函数:get_file_contents。有没有类似的或者可以给我提供一下方法吗?

我知道如何设置文件路径。就像

    set myfile to "/Users/Username/Desktop/test.txt"
    set theText to read myfile

在txt文件中,它只是一行中的一个整数,例如

    1
    2
    3
    4
    5

如果我使用

    set theTextList to paragraphs of theText

结果将带有引号“”。

我只想将这些整数设置为一个变量。这样以后我就可以使用,比如

   set variable to 5  --here I would like to replace the real number 5 to the number I read from the txt file(just one line)

   start slideshow
   show slide variable

预先感谢您的帮助。

最佳答案

如果文件的行由返回字符分隔,则应该可以使用以下内容:

set allRecords to read myfile using delimiter return
repeat with aRecord in allRecords
    if length of aRecord is greater than 0 then
        set variable to aRecord
        log "variable: " & variable
    end if
end repeat

如果文件中的字符与 return 不同,您可以更改代码第一行中的分隔字符。

关于text - Applescript,将行读取为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16044127/

相关文章:

text - 显示文本时的 Unicode 字符顺序问题

c++ - 需要更改 C++ 控制台文本大小

text - 量化文本关键字以进行神经网络分析

applescript - 如何检查applescript中是否存在进程窗口?

macos - 使用applescript发送群组iMessage

c++ - 获取组合框的文本 Win32 API C++ (NO MFC)

javascript - 单击文本时如何来回交换文本?

macos - 操作系统: check if the screen is locked

browser - 从 NodeJS 中执行 osascript(AppleScript)

swift - 在 macOS 中将 AppleScript 与 Apple Events 结合使用 - 脚本不起作用