2013-02-19 62 views
1

我正在为FoldingText设计一个script,它将FoldingText轮廓(基本上是一个Markdown文本文件)转换为一个Remark演示文稿(一个HTML脚本,可以从Markdown文件制作幻灯片)。该脚本的作品,但我想做出以下改进:保存当前文档为.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) 

感谢。

回答

0

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

我的猜测是,有一个属性像“路径”,或“文件”为FoldingText文件:

你可能会用这样的事情结束了:

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) 
+0

感谢您帮助我找到了我正在寻找的东西。目前没有错误处理,它只是将HTML附加到完整的文件名。如果文件存在,更好的脚本会提供“另存为”,并且会在追加.html之前剥离现有的.md,.txt或.ft扩展名。但现在这工作正常。 '告诉应用程序前面的文档 “FoldingText” 集thedocument提交申请的 “FoldingText” 集NEWPATH前文件,以((thedocument作为字符串)名 “.html”) 端告诉 我write_to_file(finalText,NEWPATH ,真)' – Kerim 2013-03-02 06:22:09

+0

嗯。它不会让我以我想要的方式格式化代码,5分钟后它告诉我我无法再编辑。这是修改后的脚本的[链接](http://support.foldingtext.com/discussions/questions/357-script-to-generate-remark-formatted-html-file-form-foldingtext#comment_25496591)。 – Kerim 2013-03-02 06:30:08

+0

注意:如果您运行两次,此脚本将追加,而不是覆盖现有文件,这会产生问题。不知何故需要避免。 – Kerim 2013-03-02 06:42:08

相关问题