问:将 CSV 写入变量位置
Q: Writing CSV to variable location
AppleScript 的新手,在其他地方寻找解决方案未果,如果我在其他地方错过了答案,我深表歉意。感谢您的帮助。
背景:我正在使用 AppleScript 提示用户输入目录,然后在该目录中使用其他提示创建多个文件夹。在回答提示时,我想创建一个 CSV 文件来记录这些答案以防止重复回答,还允许用户终止脚本和 return 之后不会丢失存储在列表中的项目。 CSV 似乎是最简单的方法。
问题- 如果我将 CSV 定位到静态路径,我可以成功创建 CSV 并记录到它。如果我根据提示创建路径,我会收到错误消息:Finder 出现错误:无法将别名 "Macintosh HD:Users:MyAccount:_Sample_Directory:" 转换为类型常量。数字 -1700 从别名 "Macintosh HD:Users:MyAccount:_Sample_Directory:" 到常量
我错过了什么?
仅供参考- 使用 this as basis for the WriteTo function.
示例脚本:
set MyList to {"A", "B", "C"}
tell application "Finder"
--This Works, but doesn't nest in the variable folder--
--set CSVFile to (path to desktop as text) & "List.csv"
--This is the goal:dynamic directory--
set ParentFolder to (choose folder with prompt "Select Folder")
set CSVFile to (path to ParentFolder as string & "List.csv") as POSIX file as alias
end tell
set TheResult to writeto(CSVFile, MyList, list, true)
on writeto(targetFile, theData, dataType, apendData)
try
set openFile to open for access file targetFile with write permission
if apendData is false then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access file targetFile
end try
return false
end try
end writeto
在你的脚本中,你不需要告诉应用程序"Finder"。所以你可以去掉 tell / end tell。
您脚本中的错误来自行:
set CSVFile to (path to ParentFolder as string & "List.csv") as POSIX file as alias
您的子例程 writeto 接受目标文件的格式如下:
"disk:Users:me:folder:list.csv.
因此您的第一行可以减少为:
set CSVFile to (ParentFolder as string) & "List.csv"
是的,您可以将数据写入 csv。但我不确定这是最好的选择。当您在文件中写入列表 {"A","B","C"} 时,您并不是在写入文本。所以 Applescript 进行了从列表到文本的转换。在此示例中,结果为 "listutxtAutxtButxtC"。这取决于你想对该文件做什么,但为了使其可读,最好在文本文件中写入文本。您可以通过循环遍历 MyList 的所有项目,将每一项写成文本来做到这一点:
repeat with x in MyList
set TheResult to writeto(CSVFile, x, list, true)
end repeat
在上面的例子中,我仍然使用datatype = list,而不是text。没关系,因为子例程不使用 parameter.I 猜测这是为了将来使用。
它仍然可能无法为您提供所需的结果。文件中的文本将为 "ABC"。为避免这种情况,我建议您为文件中写入的每个数据添加一个 return(或一个制表符)。为此,更改 writeto 子例程中的行:
write (theData & return) to openFile starting at eof as dataType
那么你的 cvs 文件将是
A
B
C
这很容易阅读。
AppleScript 的新手,在其他地方寻找解决方案未果,如果我在其他地方错过了答案,我深表歉意。感谢您的帮助。
背景:我正在使用 AppleScript 提示用户输入目录,然后在该目录中使用其他提示创建多个文件夹。在回答提示时,我想创建一个 CSV 文件来记录这些答案以防止重复回答,还允许用户终止脚本和 return 之后不会丢失存储在列表中的项目。 CSV 似乎是最简单的方法。
问题- 如果我将 CSV 定位到静态路径,我可以成功创建 CSV 并记录到它。如果我根据提示创建路径,我会收到错误消息:Finder 出现错误:无法将别名 "Macintosh HD:Users:MyAccount:_Sample_Directory:" 转换为类型常量。数字 -1700 从别名 "Macintosh HD:Users:MyAccount:_Sample_Directory:" 到常量
我错过了什么?
仅供参考- 使用 this as basis for the WriteTo function.
示例脚本:
set MyList to {"A", "B", "C"}
tell application "Finder"
--This Works, but doesn't nest in the variable folder--
--set CSVFile to (path to desktop as text) & "List.csv"
--This is the goal:dynamic directory--
set ParentFolder to (choose folder with prompt "Select Folder")
set CSVFile to (path to ParentFolder as string & "List.csv") as POSIX file as alias
end tell
set TheResult to writeto(CSVFile, MyList, list, true)
on writeto(targetFile, theData, dataType, apendData)
try
set openFile to open for access file targetFile with write permission
if apendData is false then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access file targetFile
end try
return false
end try
end writeto
在你的脚本中,你不需要告诉应用程序"Finder"。所以你可以去掉 tell / end tell。
您脚本中的错误来自行:
set CSVFile to (path to ParentFolder as string & "List.csv") as POSIX file as alias
您的子例程 writeto 接受目标文件的格式如下:
"disk:Users:me:folder:list.csv.
因此您的第一行可以减少为:
set CSVFile to (ParentFolder as string) & "List.csv"
是的,您可以将数据写入 csv。但我不确定这是最好的选择。当您在文件中写入列表 {"A","B","C"} 时,您并不是在写入文本。所以 Applescript 进行了从列表到文本的转换。在此示例中,结果为 "listutxtAutxtButxtC"。这取决于你想对该文件做什么,但为了使其可读,最好在文本文件中写入文本。您可以通过循环遍历 MyList 的所有项目,将每一项写成文本来做到这一点:
repeat with x in MyList
set TheResult to writeto(CSVFile, x, list, true)
end repeat
在上面的例子中,我仍然使用datatype = list,而不是text。没关系,因为子例程不使用 parameter.I 猜测这是为了将来使用。
它仍然可能无法为您提供所需的结果。文件中的文本将为 "ABC"。为避免这种情况,我建议您为文件中写入的每个数据添加一个 return(或一个制表符)。为此,更改 writeto 子例程中的行:
write (theData & return) to openFile starting at eof as dataType
那么你的 cvs 文件将是
A
B
C
这很容易阅读。