- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试将一些旧的 Applescript 移植到新的 JavaScript 语法。
有些事情似乎很简单,所以:
tell application "System Events" to keystroke "t" using command down
变成:
System = Application('System Events');
System.keystroke("t", {using: "command down"})
但是我终生无法弄清楚如何在特定位置列出文件。在 AppleScript 中,要返回 /usr
目录中的文件列表,您可以这样做:
tell application "System Events" to set fileList to name of items in folder "/usr"
-- > {"bin", "include", "lib", "libexec", "local", "sbin", "share", "standalone", "X11"}
但是我一辈子都弄不明白如何用 Javascript 来实现。
System = Application('System Events')
myPath = Path("/usr")
fileList = System.items(myPath)
-- > message not understood
fileList = System.folder(myPath)
-- > message not understood
fileList = System.diskItems(myPath)
-- > []
fileList = System.diskItems({at:myPath)
-- > []
我也尝试了很多其他组合,但没有成功。有什么想法吗?
最佳答案
赞Leopard's Scripting Bridge在此之前,JXA 故意破坏了在 AppleScript 中完美运行的各种东西。以下是将您的原始 AppleScript 命令转换为 JXA 语法:
//tell application "System Events" to name of items in folder "/usr"
Application('System Events').folders.byName('/usr').items.name()
AS 版本完美运行,但 JXA 版本只是抛出一个完全没有意义的错误 -1700:无法转换类型。
如果您编写 diskItems
而不是 items
,JXA 似乎确实有效:
Application('System Events').folders.byName('/usr').diskItems.name()
// --> ["bin", "lib", "libexec", "local", "sbin", "share", "standalone", "X11", "X11R6"]
这表明 JXA 沉迷于导致 SB 在如此多的应用程序上崩溃的内部“聪明才智”。 (请注意,我在早期的测试中发现了许多此类设计缺陷,但一旦明确 AS 开发人员只关心将他们自己的个人意识形态和偏见强加给其他人、削弱的能力和破坏的兼容性后,我就放弃了报告它们。)
为了比较,这里是 JavaScriptOSA (JOSA) prototype几个月前,我快速整理了 JXA 开发人员引用(他们立即忽略了它,natch):
app('System Events').folders.named('/usr').items.name()
// -> ["bin", "lib", "libexec", "local", "sbin", "share", "standalone", "X11", "X11R6"]
(虽然没有完全完成或测试,但 JOSA 仍然比 JXA 好得多,有更好的文档记录,甚至包括一个自动翻译工具,用于将 AS 命令转换为 JS 语法。不幸的是,因为 Apple 已经遗留或弃用了AEM、CM、PM 和 OSA Carbon API,我不推荐将其用于生产用途;它纯粹用于比较目的。)
类似地:
set myPath to POSIX file "/usr"
tell application "System Events" to name of every disk item of folder named myPath
--> {"bin", "lib", "libexec", "local", "sbin", "share", "standalone", "X11", "X11R6"}
myPath = Path('/usr')
Application('System Events').folders.byName(myPath).diskItems.name()
// Error -1728: Can't get object.
var myPath = Path('/usr')
app('System Events').folders.named(myPath).diskItems.name()
// --> ["bin", "lib", "libexec", "local", "sbin", "share", "standalone", "X11", "X11R6"]
您可以通过将 Path 对象转换为字符串并使用它来解决该特定情况:
myPath = Path('/usr')
Application('System Events').folders.byName(myPath.toString()).diskItems.name()
尽管该解决方法在其他情况下不一定有用;例如它在 Finder 中失败,因为 Finder 不理解 POSIX 样式的路径字符串,并且无法从 JXA 路径对象获取 HFS 样式的路径字符串(Finder 确实理解):
set myPath to POSIX file "/usr"
tell application "Finder" to name of every item of item myPath
--> {"X11", "X11R6", "bin", "lib", "libexec", "local", "sbin", "share", "standalone"}
myPath = Path('/usr')
Application('Finder').folders.byName(myPath.toString()).items.name()
// Error -1728: Can't get object.
就这样吧。 (例如,尝试测试 JXA 对范围、过滤器、相对和插入的支持 reference forms ;它特别糟糕。)
关于javascript - 在 Yosemite 上使用 JavaScript for Automation 列出文件夹中的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26301681/
我在使用NetBeans 6.8时遇到以下问题。我通过项目属性->库->编译选项卡->添加JAR /文件夹添加带有jar的文件夹。在下一个窗口中,我选择文件夹,然后选择“复制到库文件夹”。但是,我仍然
我的网站有一个域别名。我想知道如何将 domainA.ext 的请求重定向到 https://domainA.ext/folderA和对 domainB.ext 的请求到 http://domainB
我应该在 Eclipse 中构建的 Android 项目中创建自己的自定义菜单文件夹吗?例如,我想创建一种出现在所有 Activity 中的标题。我知道菜单应该在 res/menu 文件夹中的 XML
我正在使用 VS2008 和 .net 3.5。我在我的解决方案中创建了一个类库(Myproject.Controllers)。在这个类下,我添加了一个 Controllers 文件夹。在文件夹中我添
我有一个包含生成后步骤的 Visual Studio 2012 扩展项目,我想在其中将 .dll 和 .AddIn 文件复制到当前用户的 Visual Studio 2012 AddIns 文件夹中。
我在专有的 linux 发行版中有一些自动下载。 他们去临时暂存盘。我想在它们完成后将它们 move 到主 RAID 阵列。我能看到的最好方法是检查磁盘上的文件夹,看看内容是否在最后一分钟发生了变化。
我目前正在使用 SVN 对我的软件项目进行版本控制。在一个正在进行的项目中,我有主干,用于客户的共同功能和规范以及分支,用于客户特定的。 有没有办法在每次执行此类操作时标记一些不应合并到分支中的文
这个问题在这里已经有了答案: How to exclude a directory in find . command (45 个回答) 8 年前关闭。 如何删除文件夹中的所有内容并排除特定文件夹和文
如何在特定目录中创建具有当前日期和时间的文件夹或文件? DateTimeFormatter f = DateTimeFormatter.ofPattern("uuuuMMdd HHmmss") ; L
有没有办法在系统文件资源管理器的左侧“文件夹”栏中打开文件或文件夹?如果没有这个,我必须打开文件资源管理器并一直导航到该文件夹所在的位置才能操作文件,这确实很不方便。对于大多数带有这样导航栏的工具
预期:我使用 go get 安装包,它在 src 文件夹中创建了所有必要的文件夹,但它们只出现在 pkg/mod 文件夹中,我不能使用它们。 现实:它说它正在下载,完成,然后什么都没有。 一切都在 W
说 foo.zip包含: a b c |- c1.exe |- c2.dll |- c3.dll 哪里a, b, c是文件夹。 如果我 Expand-Archive .\foo.zip -Destin
不久前我正在删除 var 文件夹中 Magento 的缓存。我可能是错的,但我认为我犯了一个错误,而不是删除 var/cache 中的所有内容,而是意外删除了 var 中的所有内容。 Magento
我在 svn 存储库的单独文件夹中有一些代码项目。 现在我在删除文件时遇到一些问题:大多数时候一切顺利,但有时当我从磁盘删除文件或文件夹时, checkin 过程会出现各种错误。 所以我想知道:在sv
有没有什么方法可以用很少的R命令行自动删除所有文件或文件夹?我知道 unlink() 或 file.remove() 函数,但对于这些函数,您需要定义一个字符向量,其中包含您想要的文件的所有名称删除。
用于在文件夹中查找不符合Get-Childitem的LastWriteTime过滤器日期范围标准的文件的powershell命令是什么? 因此,请检查目录中是否包含不包含在01/10/2012(十月1
我正在为我工作的公司内部使用的应用程序之一编写 NSIS 安装程序,安装过程工作正常,所有 REG 键都已创建,文件夹和服务也没有问题,该应用程序使用。出于某种我无法理解的原因,卸载过程不起作用。
我有一个 Excel 文件,并且在同一文件夹中还有一个包含我想要包含的 CSV 文件的文件夹。使用“来自文件夹”查询,第一步将给出以下查询: = Folder.Files("D:\OneDrive\D
我在docker中玩ScyllaDB。为了使ScyllaDB在docker生产设置中最有效地运行,它需要一个XFS格式的磁盘。 您知道如何在Linux和MacO中创建XFS容器卷,磁盘文件吗? 谢谢
我应该编写一个函数,其中包含之前每次与该数字相乘的乘积 基本上是这样的: > productFromLeftToRight [2,3,4,5] [120,60,20,5] 我应该使用高阶函数,例如折叠
我是一名优秀的程序员,十分优秀!