在扩展坞上时使用 AppleScript 为应用程序添加自定义菜单项

发布于 2024-10-04 08:23:36 字数 114 浏览 0 评论 0原文

您可以编写 AppleScript 并将其另存为应用程序,然后将其放置在 Dock 上。但是是否可以向其中添加菜单项,以便在二次单击时显示这些菜单项?如果是这样,如何定义它们并定义它们的行为?

谢谢

You can write an AppleScript and save it as an application, and then place it on the dock. But is it possible to add menu items to it so that when secondary clicked it show these menu items? If so, how to define them, and define their actions?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两仪 2024-10-11 08:23:36

我挖出了我是怎么做到的。这适用于 leopard 和更旧的版本:它是 shell 命令的可可应用程序包装器。对于雪豹,您可以直接为苹果脚本执行此操作。

  1. Xcode->新项目...->应用-> Cocoa 应用程序
  2. (右窗格)NIB 文件 -> (右键)添加->新文件 ->可可-> Objective C 类-> “runScript.m”
  3. 修改runScript.h(右窗格):
#import Cocoa/Cocoa.h // add gt and lt (does not show here)
@interface runScript : NSObject {}
-(IBAction)runHello:(id)sender;
@end
  1. 修改runScript.m:
#import "runScript.h"
@implementation runScript
-(IBAction)runHello:(id)sender
{
    NSTask *script=[[NSTask alloc] init];
    [script setLaunchPath:@"~/Desktop/hello.app/Contents/MacOS/hello"];
    [script launch];
}
  1. NIB文件-> MainMenu.xib(双击打开界面生成器)
  2. 界面生成器->工具->库(对象页面)-> Library-cocoa-“对象和控制器”->对象(将此蓝色立方体拖放到 Main Menu.xib 窗口)
  3. 选择对象(您刚刚添加的)->界面构建器->工具->身份检查员->类->从下拉列表中选择 runScript(这是您在 2 中创建的对象 c 类)
  4. MainMenu.xib -> (双击)主菜单 -> (删除多余的 + )将“file-> new”重命名为 file->runHello
  5. 右键单击​​ runHello ->将“Actions: runHello”旁边的圆圈拖放到“运行脚本”-> runHello (在 MainMenu.xib 中)
  6. 在 Mainmenu.xib 窗口中:右键单击“文件所有者”->将dockMenu旁边的圆圈拖放到MainMenu
  7. 返回xcode ->构建->构建并运行 ->全部保存

现在,右键单击时,dock 中的程序应该有“file->runHello”项目。

您可以删除“Mainmenu.xib”中的窗口。 NSTask 还可以执行所有 shell 命令,而不仅仅是脚本。

Snow Leopard 中有一个“applescript IBAction”。您可以将“Cocoa 应用程序”更改为“Applescript 应用程序”。

br,
尤哈

I dug out how I did it. This works for leopard and older: It's a cocoa app wrapper for shell commands. For snow leopard you can do it directly for apple scripts.

  1. xcode -> new project... -> Application -> Cocoa Application
  2. (right pane) NIB Files -> (right click) Add -> New File -> Cocoa -> Objective C class -> "runScript.m"
  3. modify runScript.h (right pane):
#import Cocoa/Cocoa.h // add gt and lt (does not show here)
@interface runScript : NSObject {}
-(IBAction)runHello:(id)sender;
@end
  1. modify runScript.m:
#import "runScript.h"
@implementation runScript
-(IBAction)runHello:(id)sender
{
    NSTask *script=[[NSTask alloc] init];
    [script setLaunchPath:@"~/Desktop/hello.app/Contents/MacOS/hello"];
    [script launch];
}
  1. NIB Files -> MainMenu.xib (doubleclick opens interface builder)
  2. interface builder -> tools -> library (objects-page) -> library-cocoa-"objects & controllers" -> Object (drag and drob this blue cube to Main Menu.xib window)
  3. select Object (that you just added) -> interface builder -> tools -> identity inspector -> class -> select runScript from drop down (this is the object c class you created in 2)
  4. MainMenu.xib -> (double click) MainMenu -> (delete extra + ) rename "file-> new" to file->runHello
  5. right click runHello -> drag and drop the circle next to "Actions: runHello" to Run Script -> runHello (in MainMenu.xib)
  6. In Mainmenu.xib window: right click "File's owner" -> drag And Drop circle next to dockMenu to MainMenu
  7. Go back to xcode -> build -> build and go -> save all

Now the program in dock should have "file->runHello" item when right clicked.

You can delete the window in "Mainmenu.xib". Also NSTask can excecute all shell commands, not just scripts.

In Snow Leopard there is an "applescript IBAction". And you can change "Cocoa Application" to "Applescript application".

br,
Juha

弱骨蛰伏 2024-10-11 08:23:36

Applescript 无法做到这一点。将 Applescript 保存为应用程序实际上只消除了需要脚本编辑器来运行脚本的要求以及其他一些小的更改。说到底,它仍然只是一个剧本。如果您需要更多功能,那么您应该考虑使用 Xcode(以前称为 Applescript Studio)创建基于 Applescript 的应用程序。

You can't do this with Applescript. Saving an Applescript as an application really only removes the requirement of needing Script Editor to run the script as well as a couple other minor changes. In the end, it's still just a script. If you need more functionality, then you should look into creating Applescript-based applications with Xcode (in what used to be known as Applescript Studio).

初懵 2024-10-11 08:23:36

使用 Xcode,执行以下操作:

  1. NIB-files -> MainMenu.xib(双击)。这将打开界面生成器。
  2. 文件->新...->可可-> mainMenu
  3. 将主菜单的dockMenu 链接到“文件所有者”(第二次单击mainMenu -> 将dockMenu 圆圈拖放到文件所有者)
  4. 双击mainMenu 并编辑项目名称。将项目“发送操作”链接到您要执行的操作(与项目符号 3 中的方式相同)。

第 4 条有点不稳定,因为我不记得具体是怎么进行的。我会给您一个进一步阅读的链接:http://cocoadevcentral.com/articles/000036.php< /a>.不过,将它与 applescript 链接应该不会有任何问题。

如果您能发布一个可行的解决方案(我的意思是当您让它发挥作用时),我将不胜感激。

br,
尤哈

With Xcode, do the following:

  1. NIB-files -> MainMenu.xib (doubleclick). This opens interface builder.
  2. file -> new... -> Cocoa -> mainMenu
  3. link your mainMenu's dockMenu to "File's owner" (second click mainMenu -> drag and drop the dockMenu-circle to File's owner)
  4. double Click mainMenu and edit the item names. Link the item "send actions" to the actions you want to perform (same way as in bullet 3).

Bullet 4 is a bit shaky as I don't remember excatly how it goes. I'll give you a link for further reading: http://cocoadevcentral.com/articles/000036.php. There should not be any problems to link it with applescript, though.

I would appreciate if you could post a working solution (when you get it to work, I mean).

br,
Juha

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文