Save 使用相同的名称和路径将当前文档另存为.html

Save 使用相同的名称和路径将当前文档另存为.html,save,applescript,Save,Applescript,我正在开发一个for FoldingText,它将FoldingText大纲(基本上是一个标记文本文件)转换为演示文稿(一个HTML脚本,它可以从标记文件制作幻灯片)。脚本可以工作,但我想做以下改进: 我想获取当前文档的名称并将其保存为当前文件夹中的HTML文件(同名),而不是询问保存HTML文件的名称和位置。如果已经有一个同名的文档(提供重写该文档或另存为具有不同名称的新文档),脚本应该会很好地失败 我用来写入文件的脚本来自这些论坛。第一部分是: on write_to_file(this_d

我正在开发一个for FoldingText,它将FoldingText大纲(基本上是一个标记文本文件)转换为演示文稿(一个HTML脚本,它可以从标记文件制作幻灯片)。脚本可以工作,但我想做以下改进:

我想获取当前文档的名称并将其保存为当前文件夹中的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)

谢谢。

折叠文本应该有某种方法可以检索文档路径。我还没有找到该应用程序的任何免费下载,因此我无法亲自检查,但如果您查看该应用程序的字典,您应该能够找到它

我猜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)

多亏了你的帮助,我找到了接近我想要的东西。目前没有错误处理,它只是将HTML附加到完整的文件名上。如果文件存在,更好的脚本将提供“另存为”,并且在附加.html之前还将删除现有的.md、.txt或.ft扩展名。但是现在这个很好用<代码>告诉应用程序的前端文档“FoldingText”将文档设置为应用程序的前端文档的文件“FoldingText”将新路径设置为((文档为字符串)和“.html”)结束告诉我的写入文件(finalText,newPath,true)Hmm。它不允许我按照我想要的方式格式化代码,5分钟后它告诉我我不能再编辑了。下面是修改后的脚本的一个示例。注意:如果运行两次,此脚本将附加现有文件,而不是覆盖现有文件,这会造成问题。需要以某种方式避免这种情况。啊,通过将我的write_to_文件(finalText,newPath,true)更改为write_to_文件(finalText,newPath,false),它很容易修复!!!