将当前文档另存为具有相同名称和路径的.html

时间:2013-02-19 02:58:55

标签: save applescript

我正在为FoldingText工作script,它将FoldingText大纲(基本上是Markdown文本文件)转换为Remark演示文稿(一个HTML脚本,用于制作Markdown文件的幻灯片)。该脚本有效,但我想做出以下改进:

我想获取当前文档的名称并将其保存为当前文件夹中的HTML文件(具有相同名称),而不是要求保存HTML文件的名称和位置。如果已经有一个具有该名称的文档(提供写入文档或保存为具有不同名称的新文档),脚本应该很好地失败。

我用来写这个文件的脚本来自这些论坛。第一部分是:

on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
try
    set the target_file to the target_file as text
    set the open_target_file to ¬
        open for access file target_file with write permission
    if append_data is false then ¬
        set eof of the open_target_file to 0
    write this_data to the open_target_file starting at eof as «class utf8»
    close access the open_target_file
    return true
on error
    try
        close access file target_file
    end try
    return false
end try
end write_to_file

第二部分是:

    set theFile to choose file name with prompt "Set file name and location:"
my write_to_file(finalText, theFile, true)

感谢。

1 个答案:

答案 0 :(得分:0)

FoldingText应该有一些方法来检索文档的路径。我没有找到应用程序的任何免费下载,所以我不能自己检查,但如果你查看应用程序的字典,你应该能够找到它。

我的猜测是FoldingText文档中有'path of'或'file of'这样的属性:

你可能会得到这样的东西:

set theDocPath to file of front document of application "FoldingText"
tell application "Finder"
set theContainer to container of theFile
end tell
set newPath to (theContainer & "export.html) as string
repeat while (file newPath exists)
set newPath to choose file name with prompt "Set file name and location:"
end repeat

my write_to_file(finalText, newPath, true)