javascript 如何更改文件打开对话框的默认路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4831456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:01:26  来源:igfitidea点击:

How to Change Default Path of File Open Dialog?

javascriptscriptingshortcutadobe-indesignextendscript

提问by Kevin Coppock

I'm writing a script that allows me to do some basic imposition within InDesign CS3, and one of my tasks is to open a dialog allowing the user to select a file to place. I'm currently doing this with:

我正在编写一个脚本,允许我在 InDesign CS3 中进行一些基本的拼版,我的任务之一是打开一个对话框,允许用户选择要放置的文件。我目前正在这样做:

var file = File.openDialog("Choose a File:");

The problem seems to be when navigating to a network drive through a shortcut. For some reason, the dialog sees it as a file, and returns that path from the dialog, rather than navigating to the folder location. I'm assuming this is just a bug in the dialog, and my initial thought was to check to see if the returned file has a correct extension, and if not, display the dialog again, opened to the returned path location. However, I can't seem to find a way to change where it opens by default; openDlg()only has parameters for String prompt, var filter, and boolean multiselect. I'm not familiar with Javascript, so I don't know whether this is something with a general solution, or particular to InDesign.

问题似乎出在通过快捷方式导航到网络驱动器时。出于某种原因,对话框将其视为文件,并从对话框返回该路径,而不是导航到文件夹位置。我假设这只是对话框中的一个错误,我最初的想法是检查返回的文件是否具有正确的扩展名,如果没有,再次显示对话框,打开到返回的路径位置。但是,我似乎无法找到更改默认打开位置的方法;openDlg()只有为参数String promptvar filterboolean multiselect。我不熟悉 Javascript,所以我不知道这是通用解决方案还是 InDesign 特有的解决方案。

EDIT: If anyone's interested, here's the final code I used to get around the problem:

编辑:如果有人感兴趣,这是我用来解决问题的最终代码:

var path = new File("~/desktop");
var file = path.openDlg("Choose File:");
while (file.alias) {
    file = file.resolve().openDlg("Choose File:");
}

Basically, checks if it's a shortcut (alias), and if so, resolves the target and displays the dialog again. Kind of a hackish way to go about it, but it works just fine. May want to add some null handling in there, too, though, as if the dialog is cancelled, null is returned.

基本上,检查它是否是快捷方式(别名),如果是,则解析目标并再次显示对话框。一种hackish的方式来处理它,但它工作得很好。不过,也可能想在其中添加一些空处理,就像取消对话框一样,返回空。

采纳答案by Juan Mendes

Have you tried getting a handle to a file/directory that you want by default and calling fileHandle.openDlg()?

您是否尝试过获取默认情况下所需的文件/目录的句柄并调用fileHandle.openDlg()

http://forums.adobe.com/message/1109421#1109421

http://forums.adobe.com/message/1109421#1109421