如何在AppleScript中使用边栏? [英] How to use a sidebar with AppleScript?

查看:68
本文介绍了如何在AppleScript中使用边栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用侧边栏创建一个窗口,基本上我想要的是一个侧边栏,通过单击名称可以打开一些窗口,如下面的示例,在此先感谢.

I would like to know how I can create a window with the sidebar, basically what I want is a sidebar that will open some windows by clicking on a name, as in the example below, thanks in advance.

推荐答案

好,以下是在Xcode AppleScript应用中设置边栏的步骤.我只是在显示功能;我会让美化给你玩.

Ok, here are the steps for setting up a sidebar in an Xcode AppleScript app. I'm just showing functionality; I'll leave beautification for you to play with.

首先(显然),设置GUI.打开MainMenu.xib,然后从对象库中添加三个项目:

First (obviously), set up the GUI. Open MainMenu.xib, and add three items from the object library:

  • 窗口左侧的表格视图(以显示侧边栏)
  • 窗口右侧的自定义视图(以显示详细视图)
  • 左侧对象列表中的阵列控制器.

它应该看起来像这样(为清楚起见用红色注释表示):您将需要设置布局约束,以使边栏保持恒定的宽度,并将自定义视图固定在其侧面,但是您可以使用它.

It should look something like this (red annotation for clarity): You'll want to set up layout constraints to keep the sidebar a constant width and keep the custom view glued to its side, but you can play with that.

现在,我们将设置绑定和其他一些细节.首先,单击左侧的Array Controller对象,然后打开右侧的Utilities窗格.

Now we'll set up bindings and a few other details. First, click of the Array Controller object on the left, and open the Utilities pane on the right.

  • 单击属性"检查器.
    • 确保模式"为类"并且类名称"为NSMutableDictionary
    • 在键"部分添加两个名称:标题"和"isHeader"(这些是我们将在词典中使用的键)
    • 取消选择插入的对象",因为这很烦人.

    下一步,单击列表中的表视图",然后再次转到右侧的实用程序"窗格.

    Next, click on the Table View in the list, then go again to the Utilities pane on the right.

    • 单击属性"检查器,然后将列数设置为1.
    • 单击绑定"检查器,转到表内容"部分,然后创建两个绑定:
      • 内容中,单击绑定到复选框,从弹出菜单中选择 Array Controller ,并确保Controller Key为rangedObjects
      • 选择索引中进行相同的对象绑定,但是将 selectionIndexes 用于控制器键.
      • Click the 'Attributes' inspector and set the number of columns to 1.
      • Click the 'Bindings' inspector, go down to the Table Content section, and create two bindings:
        • In Content click the Bind to checkbox, choose Array Controller from the popup, and make sure that the Controller Key is arrangedObjects
        • In Selection Indexes make the same object binding but use selectionIndexes for the Controller Key.

        下一步,单击列表中的表格单元格视图",然后再次单击实用程序"窗格.

        Next, click on the Table Cell View in the list, and again to the Utilities pane.

        • 单击身份"检查器,并将标识符设置为"tableItem"(不带引号).这样,委托人就可以找到该特定视图并将其传递给表.

        最后,单击左侧的表视图单元格"对象(不是它上方的表单元格视图"对象,或者是它下方的第二个表视图单元格:我知道这很混乱),然后再次转到右侧的实用工具"窗格.

        Finally, click on the Table View Cell object on the left (not the Table Cell View object just above it, or the second Table View Cell just below it: confusing, I know), and go to the Utilities pane on the right, again.

        • 单击绑定"检查器,转到顶部的值"部分,然后创建一个绑定:
          • 中,单击 Bind to 复选框,从弹出菜单中选择 Table Cell View ,将Controller Key保留为空白,然后设置Model objectValue.title
          • 的关键路径
          • Click the 'Bindings' inspector, go to the Value section at top, and create one binding:
            • In Value click the Bind to checkbox, choose Table Cell View from the popup, leave the Controller Key blank, and set the Model Key Path to objectValue.title

            此方法的工作方式是将字典列表转储到Array Controller中,Table View将接起字典并将一个字典作为其ObjectValue分发给每个Table Cell View,然后Table View Cells将提取数据并显示.

            The way this works is that we'll dump a list of dictionaries into the Array Controller, the Table View will pick that up and dole out one dictionary to each Table Cell View as its objectValue, and then the Table View Cells will extract the data and present it.

            除了将脚本连接到脚本外,我们已经完成了GUI的工作.现在,转到Xcode提供的AppDelegate脚本,然后将其更改为如下所示:

            We're done with the GUI, except for connecting it the script. Now, go to the AppDelegate script that Xcode provided, and you'll want to change it to look like so:

            script AppDelegate
                property parent : class "NSObject"
            
                -- IBOutlets
                property theWindow : missing value
                property arrayController : missing value
                property detailView : missing value
            
                on applicationWillFinishLaunching_(aNotification)
                    -- Insert code here to initialize your application before any files are opened
                    (* set up list of headers and lines for the side bar *)
                    set sidebarList to {{title:"Header 1", isHeader:true}, {title:"Line 1", isHeader:false}, {title:"Header 2", isHeader:true}, {title:"Line 2", isHeader:false}, {title:"Line 3", isHeader:false}, {title:"Header 3", isHeader:true}, {title:"Line 4", isHeader:false}}
                    arrayController's addObjects:sidebarList
                end applicationWillFinishLaunching_
            
                on applicationShouldTerminate_(sender)
                    -- Insert code here to do any housekeeping before your application quits 
                    return current application's NSTerminateNow
                end applicationShouldTerminate_
            
                (* table view delegate emthods *)
            
                on tableView:tableView isGroupRow:row
                    -- header rows are group rows
                    set rowData to arrayController's arrangedObjects's objectAtIndex:row
                    return rowData's isHeader
                end
            
                on tableView:tableView shouldSelectRow:row
                    -- don't want to select header rows
                    set rowData to arrayController's arrangedObjects's objectAtIndex:row
                    return not (rowData's isHeader)
                end
            
                on tableView:tableView viewForTableColumn:column row:row
                    -- header rows get a special look
                    set aView to tableView's makeViewWithIdentifier:"tableItem" owner:me
                    return aView
                end
            
                on tableViewSelectionDidChange:aNotification
                    (* 
                     This is method gets notified right after a selection is made. This is one of 
                     the places where you can change the detail view to show a the correct view for 
                     selected sidebar item. For demonstration purposes I'm just swapping out 
                     TextField views with the name of the sidebar item. Not to sophisticated, 
                     but it get the point across
                     *)
                    set tableView to aNotification's object
                    set selectedRowIdx to (tableView's selectedRow) as integer
                    set rowData to arrayController's arrangedObjects's objectAtIndex:selectedRowIdx
                    set newLabel to current application's NSTextField's labelWithString:(rowData's title )
                    set newLabel's translatesAutoresizingMaskIntoConstraints to false
                    set detailSubviews to (detailView's subviews) as list
                    if detailSubviews is not {} then
                        set oldLabel to item 1 of detailSubviews
                        detailView's replaceSubview:oldLabel |with|:newLabel
                    else
                        detailView's addSubview:newLabel
                    end
            
                    set constraintX to newLabel's centerXAnchor's constraintEqualToAnchor:(detailView's centerXAnchor)
                    set constraintY to newLabel's centerYAnchor's constraintEqualToAnchor:(detailView's centerYAnchor)
                    constraintX's setActive:true
                    constraintY's setActive:true
                end
            
            end script
            

            我添加了两个属性-arrayController和detailView,它们的值均为'missing value'(表明它们是IBOutlets)-我们将链接到GUI.我在 applicationWillFinishLaunching _ 中添加了两行以创建记录列表,并将其添加到Array Controller中,其余的是Table View调用的委托方法.

            I added two properties — arrayController and detailView, both with the value 'missing value' (which indicates they are IBOutlets) — that we'll link up to the GUI. I've added two lines in applicationWillFinishLaunching_ to create a list of records and add it to the Array Controller, and the rest are delegate methods that the Table View calls.

            要链接GUI,请返回MainMenu.xib,单击阵列控制器"对象,在实用工具"窗格中打开连接"检查器,然后将新建引用出口"拖动到委托"对象,将其与属性连接 arrayController .对自定义视图执行相同的操作,将其连接到属性 detailView .你应该很好走.

            To link up the GUI, go back to MainMenu.xib, click on the Array Controller object, open the 'Connections' inspector in the Utilities pane, and drag a New Referencing Outlet to the Delegate object, connecting it with the property arrayController. Do the same for the Custom View, connecting it to the property detailView. You should be good to go.

            这篇关于如何在AppleScript中使用边栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆