标签: applescript

如何获取和/或覆盖 Mac OSX 中窗口的最小尺寸

我想调整我的机器上的任何窗口的大小,这是我通过 AppleScript 使用GUI Scripting完成的。该脚本类似于以下内容:

tell application "System Events"
  set appProcess to the first process whose name is "Finder"
  set appWindow to the front window of appProcess
  set position of appWindow to {100, 100}
  set size of appWindow to {10, 10}
end tell
Run Code Online (Sandbox Code Playgroud)

即使我将宽度和高度设置为 10 像素,Finder 也会将大小调整为至少 344 像素的宽度和 236 像素的高度。我用其他应用程序对此进行了测试,所有应用程序都有一个最小宽度,该宽度根据应用程序而有所不同。例如,TextMate 窗口的大小至少为 213px * 129px。

由于我有时需要将窗口大小调整为较小的宽度和高度,有没有办法覆盖这个最小尺寸,即即使我的值“太小”,也会使用它们?

如果没有,我需要知道最小尺寸,以便我知道窗口将比预期大。我将如何实现这一目标?


具体问题如下:我正在编写一个窗口平铺管理器,因此我需要负责窗口的大小。最小尺寸会产生问题:

  1. 我的图块对于某个应用程序的窗口来说可能太小,因此窗口可能会与其他图块重叠。
  2. 该窗口可能比预期更宽/更高,因此跨越了我的屏幕边界。(例如,窗口位于屏幕的右下角附近 - 我相应地设置了宽度和高度以适合角落,但由于它有最小尺寸,所以不适合。)

我很感谢任何建议。:)

最美好的祝愿
弗洛里安

macos applescript window objective-c minimum-size

6
推荐指数
1
解决办法
6242
查看次数

使用传递的参数时,防止 AppleScript 脚本更改其自己的修改日期

我有一个 AppleScript 脚本,用于通过 GeekTool 在我的桌面上显示 iTunes 曲目信息。我on runargv过去常常向脚本传递不同的参数,这样我就可以在不复制脚本的情况下显示曲目信息的不同部分(例如,我可以通过运行获得标题,我可以通过运行osascript itunes.scpt title获得艺术家/专辑osascript itunes.scpt album)。

然而,每次我用参数运行脚本时,实际的文件修改日期都会改变——似乎脚本正在向自己写入一些东西或进行某种修改。

这通常不是问题,除了在 OS X 10.7 中,Lion 为超过 2 周未修改的文件引入了文件锁定。一旦我的 iTunes 脚本被操作系统锁定,它在运行时就不能再进行这些不可见的自我更新,而且我的系统日志中充斥着这样的错误:

osascript: couldn't save changes to script /path/to/script: error -54
Run Code Online (Sandbox Code Playgroud)

我可以通过手动修改脚本来临时解决这个问题(例如,添加空行),但两周后它再次崩溃,因为 Lion 锁定了它。

理论上我可以在系统范围内禁用文件锁定来解决这个问题,但我宁愿不这样做——我喜欢它的其他用途。

那么,如何on run argv在不更改该脚本的修改日期的情况下将参数传递给 AppleScript 文件呢?

这是一个最小的工作示例。如果您从命令行 ( oscascript test.scpt blah)运行它,脚本的修改日期将会改变。

--test.scpt
on run argv
    tell application "iTunes"
        if player state is playing then
            set trck to current track
            set …
Run Code Online (Sandbox Code Playgroud)

applescript osx-lion

6
推荐指数
1
解决办法
1614
查看次数

POSIX 文件“.” 因为别名导致“CFURLGetFSRef 被传递了这个没有方案的 URL”警告

我正在使用 Araxis 提供的苹果脚本,以便我可以使用 Merge 来处理我的“git diff HEAD”命令。

但是,自从升级到 Mountain Lion 后,每次运行命令时,都会收到警告:

CFURLGetFSRef was passed this URL which has no scheme 
(the URL may not work with other CFURL routines): .
Run Code Online (Sandbox Code Playgroud)

在脚本中,似乎导致问题的原因是

set _file2 to (POSIX path of (POSIX file "." as alias)) & _file2
Run Code Online (Sandbox Code Playgroud)

当我对路径进行硬编码而不是使用时".",警告就会消失。我试过 pre-pending file:///,但POSIX path of正在考虑将其中一个斜杠作为转义字符,因此路径变为file:/Users/...然后由于具有未知模式而产生错误(因为第二个斜杠已被删除)。所以,我的问题是,获取POSIX file当前位置的正确方法是什么(现在,在 Mountain Lion 中)?

添加完整脚本

#!/usr/bin/osascript
# This script is required to reorder the parameters sent by Git, so that they may …
Run Code Online (Sandbox Code Playgroud)

git merge applescript araxis

6
推荐指数
2
解决办法
1678
查看次数

AppleScript 处理程序中的可选参数

Applescript文档表示,从 Yosemite 开始,处理程序的参数可以是可选的。

从“参数规格”部分:

带标签的参数可以通过在形式参数名称后面加上 :literal 来声明默认值。这样做会使参数在调用时成为可选参数。例如,这声明了一个 make 处理程序,其 with data 参数具有默认值:

on make new theClass with data theData : missing value

现在可以在不提供 with data 参数的情况下调用该处理程序;处理程序将看到数据设置为指定的默认缺失值,然后可以对其进行测试并进行适当的处​​理。

因此,由于需要一个带有可选参数的处理程序,我尝试创建一个。我已经做到了这一点:

set theResult to Create of me given the string:"stuff", info:"this"

on Create given info:missing value, thestring:"stuff"
    if info is missing value then
        set present to false
    else
        set present to true
    end if
    return {present, thestring}
end Create
Run Code Online (Sandbox Code Playgroud)

它可以编译,但给出错误“变量 thestring 未定义。”

如果我只用一个参数调用它:

set theResult to Create of me given thestring:"stuff"
Run Code Online (Sandbox Code Playgroud)

我收到错误:“创建时缺少信息参数。” …

applescript function handler optional-parameters

6
推荐指数
2
解决办法
3712
查看次数

AppleScript 获取窗口标题

我尝试获取应用程序的标题(并将其复制为 var)\n这是到目前为止我的代码,但失败了

\n\n
tell application "app"\n    activate\nend tell\n\ntell application "System Events"\n    set frontApp to name of first application process whose frontmost is true\nend tell\n\ntell application frontApp\n    if the (count of windows) is not 0 then\n        set window_name to name of front window\n    end if\nend tell\n
Run Code Online (Sandbox Code Playgroud)\n\n

结果 :

\n\n
\n

错误“应用程序出现错误:每个窗口\xe2\x80\x99无法理解\xe2\x80\x9ccount\xe2\x80\x9d消息。” 每个窗口的数字 -1708

\n
\n

windows applescript title

6
推荐指数
1
解决办法
1万
查看次数

使用 JXA AppleScript 获取活动 Finder 窗口的 POSIX 路径

我想要这个 AppleScript 片段的 JXA 等效项:

tell application "Finder"

    # Get path
    set currentTarget to target of window 1
    set posixPath to (POSIX path of (currentTarget as alias))

    # Show dialog
    display dialog posixPath buttons {"OK"}

end tell
Run Code Online (Sandbox Code Playgroud)

我得到的最接近的是使用该url属性来初始化Foundation NSURL对象并访问其fileSystemRepresentation属性,如下所示:

// Get path
var finder = Application('Finder')
var currentTarget = finder.finderWindows[0].target()
var fileURLString = currentTarget.url()

// I'd like to get rid of this step
var fileURL = $.NSURL.alloc.initWithString(fileURLString)
var posixPath = fileURL.fileSystemRepresentation

// Show dialog …
Run Code Online (Sandbox Code Playgroud)

applescript finder javascript-automation macos-sierra

6
推荐指数
1
解决办法
2299
查看次数

以编程方式从文本数据生成 Keynote 幻灯片

我编写了一个数据库 Web 应用程序来处理教堂礼拜歌曲,并且我正在尝试添加一个模块来输出选定的歌曲以投影歌词。我最初以为我所有的用户都在使用 Powerpoint,它可以导入一个简单的文本文件,每行前面有 0-5 个选项卡(0 个选项卡行成为新幻灯片的标题,一个或多个选项卡意味着一个“项目符号点”)级别对应于多少个选项卡)。因此,我的模块当前输出这样一个文本文件,演示者将打开一个按照他们想要的歌曲演示方式设计的 Powerpoint 模板,插入文本文件作为新幻灯片的“大纲”,然后瞧。下面是在 Powerpoint 中使用的文本文件结构的一个小示例(其中一张日语幻灯片,通过利用下一级大纲以较小字体进行罗马化):

\n\n
As the Deer\n    As the deer panteth for the water\n    So my soul longeth after Thee\n    You alone are my heart\'s desire\n    And I long to worship Thee\nAs the Deer\n    You alone are my strength, my shield\n    To You alone may my spirit yield\n    You alone are my heart\'s desire\n    And I long to worship Thee\n\xe9\xb9\xbf\xe3\x81\xae\xe3\x82\x88\xe3\x81\x86\xe3\x81\xab (As the Deer)\n    \xe8\xb0\xb7\xe5\xb7\x9d\xe3\x81\xae\xe6\xb5\x81\xe3\x82\x8c\xe3\x82\x92\xe3\x80\x80\xe3\x81\x97\xe3\x81\x9f\xe3\x81\x86\xe9\xb9\xbf\xe3\x81\xae\xe3\x82\x88\xe3\x81\x86\xe3\x81\xab\n        tanigawa no nagare o shitau shika no yoo …
Run Code Online (Sandbox Code Playgroud)

pdf applescript text keynote

6
推荐指数
1
解决办法
4486
查看次数

AppleScript 读取活动应用程序名称、标题和 URL

我需要 AppleScript,当从命令行运行时,它会返回三件事:

  1. 当前活动的应用程序名称;前任。“Google Chrome”、“Safari”、“Steam”、“iTunes”、“Atom”等。
  2. 当前活动的应用程序标题(如果有)
  3. 如果当前活动应用程序是浏览器,我需要当前活动选项卡 URL

例子:

我知道还有一些类似的问题。stackoverflow 上的答案在这里,但我无法将它们组合在一起以完成所有这些工作。非常感谢您的帮助。

macos scripting command-line applescript osascript

6
推荐指数
1
解决办法
2998
查看次数

Applescript 单击状态栏应用程序的菜单项

我有一个名为 Fen\xc3\xaatre 的应用程序,当使用命令查看进程名称时,top它给出了名称Fene?~Btre H。

\n\n

我想单击其菜单栏项下名为“a.py”的项目,如图所示。

\n\n

在此输入图像描述\n在此输入图像描述

\n\n

我的尝试:

\n\n

尝试1

\n\n
tell application "System Events" to tell process "Fen\xc3\xaatre"\n    tell menu bar item 1 of menu bar 1\n        click\n        click menu item "Show all" of menu 1\n    end tell\nend tell\n
Run Code Online (Sandbox Code Playgroud)\n\n

错误:

\n\n
$ osascript a.applescript \na.applescript:121:157: execution error: System Events got an error: Can\xe2\x80\x99t get menu item "Show all" of menu 1 of menu bar item 1 of menu bar 1 of process …
Run Code Online (Sandbox Code Playgroud)

macos applescript menubar

6
推荐指数
1
解决办法
3744
查看次数

AppleScript 浏览 MacOS 13 (Ventura) 系统首选项?

macOS 13 Ventura 的一项新功能是系统偏好设置的新布局和设计。然而,这导致我的许多自动化脚本停止工作。

有谁知道如何浏览新的系统偏好设置?准确地说,与 macOS 13 Ventura 兼容的以下代码等效于什么?

tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.sound"
end tell
Run Code Online (Sandbox Code Playgroud)

上面的代码适用于旧版本,但显示

执行错误:系统设置出现错误:AppleEvent 处理程序失败。(-10000)

在 macOS 13 文图拉中

macos applescript macos-ventura

6
推荐指数
1
解决办法
2563
查看次数