【问题标题】:How to use a sidebar with AppleScript?如何在 AppleScript 中使用侧边栏?
【发布时间】:2019-08-13 14:05:15
【问题描述】:

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

【问题讨论】:

  • 这是什么应用程序?使用 GUI 脚本在侧边栏选项卡中旋转很容易,但实施取决于目标应用程序。
  • @TedWrigley 这个示例应用程序是 CleanMyMac,我想将侧边栏用于简单的事情,我喜欢侧边栏主要使用 CleanMyMac 完成的事情想知道使用 AppleScript 的简单方法。
  • @TedWrigley,你能帮我解决这个问题吗?我只想滚动浏览 5 个屏幕。
  • 是的,我可以做到,稍后我会发布 CleanMyMac 应用程序的答案,但您应该知道,您尝试使用的每个应用程序的代码都会有所不同.没有普遍的答案。我会在帖子中解释。
  • @Ted wrigle,有什么消息吗?

标签: applescript


【解决方案1】:

好的,以下是在 Xcode AppleScript 应用程序中设置侧边栏的步骤。我只是展示功能;我把美化留给你玩。

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

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

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

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

  • 单击“属性”检查器。
    • 确保 Mode 为“class”,“class name”为 NSMutableDictionary
    • 在“Keys”部分添加两个名称:“title”和“isHeader”(这些是我们将在字典中使用的键)
    • 取消单击“选择插入的对象”,因为它很烦人。

接下来,单击列表中的表格视图,然后再次转到右侧的实用程序窗格。

  • 单击“属性”检查器并将列数设置为 1。
  • 单击“绑定”检查器,转到“表格内容”部分,然后创建两个绑定:
    • Content 中单击 Bind to 复选框,从弹出窗口中选择 Array Controller,并确保 Controller Key 为 排列对象
    • Selection Indexes 中进行相同的对象绑定,但使用 selectionIndexes 作为控制器键。
  • 单击“Connections”检查器,然后从委托出口拖到左侧的 Delegate 对象,这样表格视图将使用 AppDelegate 脚本作为其表格视图委托。

接下来,单击列表中的表格单元格视图,然后再次进入实用程序窗格。

  • 单击“Identity”检查器并将标识符设置为“tableItem”(不带引号)。这让代理可以找到这个特定的视图并将其传递给表格。

最后,单击左侧的 Table View Cell 对象(不是它正上方的 Table Cell View 对象,或者它正下方的第二个 Table View Cell:令人困惑,我知道),然后再次转到右侧的实用程序窗格。

  • 单击“绑定”检查器,转到顶部的“值”部分,然后创建一个绑定:
    • Value 中单击 Bind to 复选框,从弹出窗口中选择 Table Cell View,将 Controller Key 留空,然后设置 Model objectValue.title 的关键路径

它的工作方式是我们将一个字典列表转储到数组控制器中,表格视图将拾取它并将一个字典分配给每个表格单元视图作为其对象值,然后表格视图单元将提取数据并呈现出来。

我们已经完成了 GUI,除了将它连接到脚本。现在,转到 Xcode 提供的 AppDelegate 脚本,您需要将其更改为如下所示:

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,它们的值都是“缺失值”(表示它们是 IBOutlets)——我们将链接到 GUI。我在applicationWillFinishLaunching_ 中添加了两行,用于创建记录列表并将其添加到 Array Controller,其余的是 Table View 调用的委托方法。

要链接 GUI,请返回 MainMenu.xib,单击 Array Controller 对象,在 Utilities 窗格中打开“Connections”检查器,然后将 New Referencing Outlet 拖动到 Delegate 对象,将其与属性连接数组控制器。对自定义视图执行相同的操作,将其连接到属性 detailView。你应该很高兴。

【讨论】:

  • 对不起,我想我在这里不清楚,我想创建一个带有侧边栏的应用程序,你给我的例子和这个?
  • 很抱歉,是的,我没有从您的帖子中得知。您想在 AppleScript 中创建一个带有侧边栏的窗口,是吗?
  • 是的,我想创建一个带侧边栏的窗口,如果我不是很清楚,很抱歉。
  • 您能否重写问题以使其更清楚?
  • 感谢您的辛勤工作,我明天试试,并将结果发布在这里。
【解决方案2】:

您正在寻找的是 sidebar / source list - 您可以查看 Apple 较早的 SidebarDemo 示例项目,以获取使用大纲视图的相当小的示例(甚至可能相对容易转换为 ASObjC)。

根据你最终想要做什么,我想它可以用 AppleScript 来完成,但我认为你会发现应用程序越大,痛苦就越大。 AppleScript 非常适合制作原型和控制其他应用程序,但当事情开始变得庞大或复杂时(例如,您以前使用工作表的经验)就不是那么好了。如果你打算做一些更严肃的应用程序,你可能会考虑使用一种也更严肃的语言。

【讨论】:

  • 我真的很想知道在AppleScript中使用侧边栏的简单方法,关于工作表我仍然面临一些困难,但我稍后会在适当的主题中问你。
猜你喜欢
  • 2010-09-23
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 2014-07-10
  • 2012-01-13
  • 2021-08-11
  • 2023-02-26
  • 1970-01-01
相关资源
最近更新 更多