2012-08-04 74 views
3

有什么办法可以将下面的苹果脚本转换为Objective-C/C吗?从Objective-C/C发送系统事件的请求?

tell application "System Events" to set visible of process "Safari" to false 

我知道我可以使用NSAppleScript类或调用system("osascript -e '...'")在Objective-C执行该AppleScript的,但是是不是有另一种方式?

苹果如何做到这一点?

或者,我可以从Objective-C/C的另一个应用程序隐藏窗口吗?

更新:

我发现,您可以使用SBApplication类来做到这一点:

SBApplication *SystemEvents = [SBApplication applicationWithBundleIdentifier:@"com.apple.systemevents"]; 
/*SystemEventsApplicationProcess*/ id Safari = [[SystemEvents performSelector:@selector(applicationProcesses)] objectWithName:@"Safari"]; 
[Safari setVisible:NO]; // Doesn't work! 

但是这不工作为setVisible可能没有做什么,我想。

这是SystemEventsApplicationProcess的类层次结构:

SystemEventsApplicationProcess : SystemEventsProcess : SystemEventsUIElement : SystemEventsItem : SBObject : NSObject 

这里是这些SystemEventsXXX类可用的方法:

SystemEventsApplicationProcess 
    applicationFile 

SystemEventsProcess 
    setVisible: 
    visible 
    unixId 
    totalPartitionSize 
    shortName 
    partitionSpaceUsed 
    name 
    id 
    hasScriptingTerminology 
    setFrontmost: 
    frontmost 
    fileType 
    file 
    displayedName 
    creatorType 
    Classic 
    bundleIdentifier 
    backgroundOnly 
    architecture 
    acceptsRemoteEvents 
    acceptsHighLevelEvents 
    windows 
    menuBars 

SystemEventsUIElement 
    select 
    clickAt: 
    setValue: 
    value 
    title 
    subrole 
    setSize: 
    size 
    setSelected: 
    selected 
    roleDescription 
    role 
    setPosition: 
    position 
    orientation 
    name 
    minimumValue 
    maximumValue 
    help 
    setFocused: 
    focused 
    entireContents 
    enabled 
    objectDescription 
    objectClass 
    accessibilityDescription 
    windows 
    valueIndicators 
    UIElements 
    toolBars 
    textFields 
    textAreas 
    tables 
    tabGroups 
    staticTexts 
    splitterGroups 
    splitters 
    sliders 
    sheets 
    scrollBars 
    scrollAreas 
    rows 
    relevanceIndicators 
    radioGroups 
    radioButtons 
    progressIndicators 
    popUpButtons 
    popOvers 
    outlines 
    menuItems 
    menuButtons 
    menuBarItems 
    menuBars 
    menus 
    lists 
    incrementors 
    images 
    growAreas 
    groups 
    drawers 
    comboBoxes 
    columns 
    colorWells 
    checkboxes 
    buttons 
    busyIndicators 
    browsers 
    attributes 
    actions 

SystemEventsItem 
    setName: 
    name 
    id 
    removeActionFromUsingActionName:usingActionNumber: 
    pick 
    keyUp 
    keyDown 
    increment 
    editActionOfUsingActionName:usingActionNumber: 
    doScript 
    doFolderActionFolderActionCode:withItemList:withWindowSize: 
    decrement 
    confirm 
    cancel 
    attachedScripts 
    attachActionToUsing: 
    stop 
    start 
    saveAs:in: 
    moveTo: 
    exists 
    duplicateTo:withProperties: 
    delete 
    closeSaving:savingIn: 
    setProperties: 
    properties 
    objectClass 

SBObject 
    // ... 
NSObject 
    // ... 
+0

曾经有一个碳功能,'AESend()',对于这一点,但它在碳的过时的部分。有传言说,碳位仍然为可可的AppleEvents系统的基础,但考虑到即使文档“苹果事件编程指南”和“苹果事件管理指南”已经消失,似乎'NSAppleScript'或'SBApplication'是前进的道路。 – 2012-08-04 19:46:54

回答

2

您可以使用NSRunningApplication,它代表(顾名思义)一个正在运行的应用程序,并有一个-hide方法。

NSWorkspace会给你所有正在运行的应用程序的列表:[[NSWorkspace sharedWorkspace] runningApplications],您可以过滤,或者你可以使用其捆绑标识符代表Safari中的对象:+[NSRunningApplication runningApplicationsWithBundleIdentifier:](注意实际返回的情况下有多个正在运行的阵列同一个应用的实例)。

2

除非您将脚本桥架构添加到您的项目和其他一些事情,否则代码将无法正常工作。你有没有做过......我无法分辨。如果您需要说明,This link似乎对所需内容有很好的解释。

顺便说一句,“设置可见”意味着隐藏应用程序,就像隐藏应用程序菜单一样。但是,如果你想隐藏一个应用程序,我确定有一个NSWorkspace方法。

最后一点建议......只有几行的苹果代码NSApplescript将是您的最佳选择。如果你打算使用大量的AppleScript脚本代码,则脚本桥是更好的选择,虽然我自己经常只是把编译脚本在我的项目,然后用NSApplescript从该脚本启动处理程序。你也可以使用ApplescriptObjC语言。你有很多选择。