【问题标题】:AppleScript how to write a file path to a text fileAppleScript如何将文件路径写入文本文件
【发布时间】:2015-07-24 21:38:54
【问题描述】:

我即将为我的 AppleScript 构建一个新功能。

我希望能够提示用户选择一个 Excel 文件,然后处理该 Excel 文件。

新功能是我想存储用户上次选择的文件的文件路径,以便下次执行脚本时对话框打开到同一个文件夹。

我的对话框工作正常,文件写入工作也正常工作。

我的问题是我希望能够将文件路径写入文本文件,但我不知道如何。

考虑以下代码:

set theFile to choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"}
display dialog (theFile as string)

set outputFile to (("Macintosh HD:Users:lowken:Documents:") & "LaunchAgent_Alert.txt") 

try
    set fileReference to open for access file outputFile with write permission
    write theFile to fileReference
    close access fileReference
on error
    try
        close access file outputFile
    end try
end try

代码有效,但输出文件中出现垃圾:

>Macintosh HDÀ·q†H+÷œMiamieMasterMind.xlsxó∑èœÇäRXLSXXCELˇˇˇˇI À·©‡œÇ¬í,MiamieMasterMind.xlsxMacintosh HD*Users/lowken/Dropbox/MiamieMasterMind.xlsx/
ˇˇ

我的猜测是我有文件编码问题,或者我需要从 theFile 输出文件路径。

感谢您的帮助。

【问题讨论】:

    标签: applescript


    【解决方案1】:

    克雷格解释的财产使用是最简单的解决方案。 如果您重新编译脚本,属性值将被重置。

    但是,如果你真的需要将路径值存储在txt文件中以供其他脚本使用,你只需要编写文件,而不是别名,而是字符串:

    write (theFile as string) to fileReference
    

    当然,以后读取文本文件的时候,记住是字符串而不是别名!

    【讨论】:

    • 虽然 Craig 提供了一个很好的解决方案,但您回答了我提出的问题。谢谢
    【解决方案2】:

    尝试使用属性,脚本将为您完成所有工作:

    property theContainer : null
    property theFile : null
    
    set theFile to choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"}
    tell application "Finder" to set theContainer to theFile's container
    

    来自 AppleScript 语言指南:

    属性的值在脚本之后仍然存在 属性已定义已运行。因此,currentCount 的值为 0 此脚本第一次运行时,下次运行时为 1,依此类推 上。属性的当前值与脚本对象一起保存,并且 在重新编译脚本之前不会重置为 0,也就是说,修改和 然后再次运行、保存或检查语法。

    【讨论】:

      【解决方案3】:

      您可以保存一个 appleScript 的类并将其读取为(类型类)。

      例子

      1. write theFile to fileReference — theFile 是 appleScript 的别名

        这样读——>set theFile to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as alias

      2. 如果您保存列表:

        write myList to fileReference — myList 是一个 appleScript的列表

        这样读——>set myList to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as list

      3. 如果您保存记录 —> {b: "15", c:"éèà"} :

        write myRecord to fileReference — myRecord 是 appleScript 的 记录

        这样读——>set myRecord to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as record

      4. 如果你保存一个真实的——> 200.123

        write floatNumber to fileReference — floatNumber 是 appleScript 的 编号

        这样读——>set floatNumber to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as real

      5. 如果你保存一个整数——>20099

        write xNum to fileReference — xNum 是 appleScript 的整数

        这样读——>set xNum to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as integer

      6. 如果你保存一个字符串——> "éèà:376rrrr" :

        write t to fileReference — t 是 appleScript 的字符串

        这样读——>set t to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as string


      重要提示:set eof to 0 在将新内容写入现有文件之前

      set fileReference to open for access file outputFile with write permission
      set eof fileReference to 0
      write something to fileReference
      close access fileReference
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多