AppleScript 如何将文件路径写入文本文件
AppleScript how to write a file path to a text file
我即将为我的 AppleScript 构建新功能。
我希望能够提示用户 select 一个 Excel 文件,然后处理该 Excel 文件。
新功能是我想存储用户上次 selected 文件的文件路径,以便下次执行脚本时对话框打开到同一文件夹。
我的对话框正常工作,文件写入部分也正常工作。
我的问题是我希望能够将文件路径写入文本文件,但我不知道该怎么做。
考虑以下代码:
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 输出文件路径。
感谢您的帮助。
尝试使用 属性,脚本将为您完成所有工作:
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 语言指南:
The value of a property persists after the script in which the
property is defined has been run. Thus, the value of currentCount is 0
the first time this script is run, 1 the next time it is run, and so
on. The property’s current value is saved with the script object and
is not reset to 0 until the script is recompiled—that is, modified and
then run again, saved, or checked for syntax.
Craig 解释的 属性 用法是最简单的解决方案。
如果您重新编译脚本,属性 值将被重置。
但是,如果你真的需要将路径值存储在txt文件中以供其他脚本使用,你只需要将文件写入,而不是作为别名,而是作为字符串:
write (theFile as string) to fileReference
当然,以后阅读文本文件时,记住它是一个字符串,而不是别名!
您可以保存一个 appleScript 的 class 并将其读取为(类型 class)。
例子
write theFile to fileReference
— theFile 是 appleScript 的别名
这样读 —> set theFile to read file "Macintosh
HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as alias
如果保存列表:
write myList to fileReference
— myList 是一个
appleScript 的列表
这样读 —> set myList to read file "Macintosh
HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as list
如果保存一条记录 —> {b: "15", c:"éèà"} :
write myRecord to fileReference
— myRecord 是 appleScript 的
记录
这样读 —> set myRecord to read file "Macintosh
HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as record
如果存一个真实的—>200.123:
write floatNumber to fileReference
— floatNumber 是 appleScript 的
数量
这样读 —> set floatNumber to read file "Macintosh
HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as real
如果你存的是整数 —> 20099 :
write xNum to fileReference
— xNum 是 appleScript 的整数
这样读 —> set xNum to read file "Macintosh
HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as integer
如果你保存的是字符串 —> "éèà: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
我即将为我的 AppleScript 构建新功能。
我希望能够提示用户 select 一个 Excel 文件,然后处理该 Excel 文件。
新功能是我想存储用户上次 selected 文件的文件路径,以便下次执行脚本时对话框打开到同一文件夹。
我的对话框正常工作,文件写入部分也正常工作。
我的问题是我希望能够将文件路径写入文本文件,但我不知道该怎么做。
考虑以下代码:
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 输出文件路径。
感谢您的帮助。
尝试使用 属性,脚本将为您完成所有工作:
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 语言指南:
The value of a property persists after the script in which the property is defined has been run. Thus, the value of currentCount is 0 the first time this script is run, 1 the next time it is run, and so on. The property’s current value is saved with the script object and is not reset to 0 until the script is recompiled—that is, modified and then run again, saved, or checked for syntax.
Craig 解释的 属性 用法是最简单的解决方案。 如果您重新编译脚本,属性 值将被重置。
但是,如果你真的需要将路径值存储在txt文件中以供其他脚本使用,你只需要将文件写入,而不是作为别名,而是作为字符串:
write (theFile as string) to fileReference
当然,以后阅读文本文件时,记住它是一个字符串,而不是别名!
您可以保存一个 appleScript 的 class 并将其读取为(类型 class)。
例子
write theFile to fileReference
— theFile 是 appleScript 的别名这样读 —>
set theFile to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as alias
如果保存列表:
write myList to fileReference
— myList 是一个 appleScript 的列表这样读 —>
set myList to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as list
如果保存一条记录 —> {b: "15", c:"éèà"} :
write myRecord to fileReference
— myRecord 是 appleScript 的 记录这样读 —>
set myRecord to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as record
如果存一个真实的—>200.123:
write floatNumber to fileReference
— floatNumber 是 appleScript 的 数量这样读 —>
set floatNumber to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as real
如果你存的是整数 —> 20099 :
write xNum to fileReference
— xNum 是 appleScript 的整数这样读 —>
set xNum to read file "Macintosh HD:Users:lowken:Documents:LaunchAgent_Alert.txt" as integer
如果你保存的是字符串 —> "éèà: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