当前位置: 首页 > 知识库问答 >
问题:

取消选择所有在Finder使用JavaScript的自动化

尉迟墨竹
2023-03-14

在使用JavaScript实现自动化时,我发现自己无法用AppleScript做一些非常简单的事情。(令人震惊,我知道。)

这个AppleScript:

tell application "Finder" to set selection to {}

清除Finder中的选择。

我只是不知道如何在JXA中实现同样的功能。

这是我尝试过的:

var finder = Application("Finder")
finder.includeStandardAdditions = true
  //this selects files in the front window...
finder.select( [...array of file paths...] )
  //so you'd think this might work to deselect all...
finder.select( [] )
  //...but it doesn't do anything

//then I tried each of these in turn...

finder.select( null )
  //Error -10010: Handler can't handle objects of this class.

finder.selection = null
  //Error -10010: Handler can't handle objects of this class.

finder.selection = []
  //Script Editor crashes

//...but none were successful

有什么建议吗?

(macOS Sierra,脚本编辑器2.9)

共有1个答案

令狐增
2023-03-14

这是另一个你可以更好地考虑的“工作”。这将通过do shell通过JXA调用的osascript发送脚本的AppleScript版本(诀窍当然是正确地获取转义字符):

var app = Application.currentApplication();
app.includeStandardAdditions = true;

app.doShellScript('osascript -e "tell application \\"Finder\\" to set selection to {}"');

------------原始答案-------------

好吧,我很确定我应该为这个答案道歉,但是,好吧,它确实有效,通过一个奇怪的工作环境,因为在玩了几个小时后,我无法得到一个更优雅的解决方案。谁知道呢,也许你会觉得这很优雅。我几乎做到了。或者也许有人会加入一个更好的。而且,我只是在脚本编辑器中处理过这个问题。[编辑]哦,还有,我还在10.10.5上

//this allows use of JXA version of do shell script later
var app = Application.currentApplication();

app.includeStandardAdditions = true;
var thefinder = Application("Finder");
//Would advise using if/then here to check if there's at least one window, like:
//    if (thefinder.windows.length != 0) {

//gets front window (where selection is, always)
var fWin = thefinder.windows[0];

//gets the "target", which is the folder ref
var fWinTarget = fWin.target();

//gets the url of the target
var winU = fWinTarget.url();

//makes that a path
var winUPath = Path(winU);

//closes the original finder window (ref)
fWin.close();

//opens it up again! no more selection!
app.doShellScript("open " + winU);
 类似资料:
  • 问题内容: 我正在寻找完全像这些的东西(带有“父母”的三态复选框)。但是使用该解决方案并不好用,因为我现在不依赖jQuery,因此我需要调用$ scope。$ apply来使模型识别自动(未选中)的复选框单击的jQuery。 这是angular.js的错误,要求执行ng- indeterminate-value。但这仍然不能使我同步到所有孩子,这是我认为不应该成为我的控制器的一部分的事情。 我正在

  • 在粗略的破折号中,可以根据单击图例项来选择行。一开始他们都是预选的。是否可以在仪表板开始时取消选择所有这些选项?谢啦

  • 我想创建一些表格与自动填充价格在输入框,但我不知道如何实现到我的代码。首先我有选择选项,然后选择选项后,输入框上的价格会自动生成,然后我输入折扣值,然后总价格会自动生成(基本价格-折扣%)。这是我的代码: null null

  • 我有一个超文本标记语言选择框,在我的jsp中启用了多选择。我从其他按钮动态填充选项。因此,用户不会从该选择框中选择任何值。我必须将selectbox的所有值传递给其他jsp。 我正在使用表单操作提交浏览jsp。这样做时,我无法获得所有的选择框值。 此选项仅提供该多选框中的选定值。我想要的是从selectbox中获取所有值,无论是否选中。 谢谢

  • 问题内容: 我需要有关jQuery选择器的帮助。假设我有一个标记,如下所示: 除了用户单击时,如何获取所有复选框? 问题答案: 一个更完整的示例适用于您的情况: 当被点击复选框,该复选框的状态进行检查,并在当前形式的所有复选框被设置为相同的状态。 请注意,您无需从选择中排除该复选框,因为该复选框的状态将与其他所有复选框相同。如果出于某些原因确实需要排除,则可以使用以下方法:

  • 我正在开发一个使用JTree的java应用程序。我想归档的是,当我点击一个已经被选中的节点时,它会被取消选中。 我目前的解决方案是向jtree添加鼠标侦听器和树选择侦听器。但问题是,valueChanged只有在选择发生更改时才会被调用(而不是在两次选择同一节点时)。为了解决这个问题,我添加了一个布尔值,它指示是否第一次单击了节点,然后我在MouseRelease函数中处理取消选择。这是可行的,但