简体   繁体   English

如何在 Mac AppleScript 中从 PID 获取要调用的应用程序名称

[英]How to get Application Name To Call from PID in Mac AppleScript

There is a script that lets you resize any app in mac.有一个脚本可以让您在 mac 中调整任何应用程序的大小。 This is the code:这是代码:

set theApp to "Application Name" 
set appHeight to 1080
set appWidth to 1920

    tell application "Finder"
    set screenResolution to bounds of window of desktop
end tell

set screenWidth to item 3 of screenResolution
set screenHeight to item 4 of screenResolution

tell application theApp
      activate
      reopen
      set yAxis to (screenHeight - appHeight) / 2 as integer
      set xAxis to (screenWidth - appWidth) / 2 as integer
      set the bounds of the first window to {xAxis, yAxis, appWidth + xAxis, appHeight + yAxis}
      end tell

I want to change the size of a java application opened by a launcher.我想更改启动器打开的 java 应用程序的大小。 When I insert the name of any app, it works.当我插入任何应用程序的名称时,它就可以工作。 However when I insert the name of the app that I want to resize it doesn't work.但是,当我插入要调整大小的应用程序的名称时,它不起作用。 I know the process id of the app that I want to resize.我知道要调整大小的应用程序的进程 ID。 Is there a way I can change this line set theApp to "Application Name" to use PID instead of Application name?有没有办法可以将这一行set theApp to "Application Name"以使用 PID 而不是应用程序名称? Thanks.谢谢。

Not all apps are AppleScript scriptable and some that are do not support the bounds property , they use position property and size property .并非所有应用程序都可以编写AppleScript脚本,有些应用程序不支持bounds属性,它们使用position属性size属性 Also, sometimes you'll need System Events to position and size an app's window .此外,有时您需要position系统事件并调整应用程序的window大小

I use a keyboard shortcut assigned in FastScripts with the following example AppleScript code to automatically adjust the frontmost app's window .我使用在FastScripts中分配的键盘快捷键和以下示例AppleScript代码来自动调整最前面的应用程序的window You can adjust the code to suite your needs.您可以调整代码以满足您的需求。

If the frontmost app can't use the bounds property it silently errors , and then System Events does it.如果最前面应用程序不能使用bounds属性,它会默默地出错,然后系统事件会这样做。

tell application "System Events"
    set frontmostProcess to name of process 1 whose frontmost is true
end tell

try
    tell application frontmostProcess
        set bounds of window 1 to {0, 22, 1136, 844}
    end tell
on error
    tell application "System Events" to tell application process frontmostProcess
        set position of window 1 to {0, 22}
        set size of window 1 to {1136, 822}
    end tell
end try

Note: I am not affiliated with the developer of FastScript, just a satisfied user.注意:我不隶属于 FastScript 的开发者,只是一个满意的用户。 It's also free for the first ten keyboard shortcuts .前十个键盘快捷键也是免费的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM