Script to select workspace

I use the Workspace command a lot, but I’m always hunting for the menu, or forget which key command I assigned to a specific workspace. So I wanted a button in the toolbar that would give me quick access to any of my workspaces. I made this quick little script, and saved in into the Scripts > Toolbar folder:

tell application id "DNtp"
  try
    set theWorkspaces to workspaces
    choose from list theWorkspaces
        if the result is not false then
          set theSelection to item 1 of the result
          load workspace theSelection
        end if
  end try
end tell
2 Likes

Very nice… (And because I’m infamous for going for the one-liner :stuck_out_tongue: ), it could also be done with this…

tell application id "DNtp" to load workspace ((choose from list (workspaces as list)) as string)

Bear in mind, your approach is definitely more suitable for teaching situations. Mine is an attempt to be as succinct, yet stable, as possible. :slight_smile:

3 Likes

Show off. :stuck_out_tongue:

OK, like yours better.

1 Like

Hahaha! :wink:

But as I said, yours is actually more useful to teach people basic AppleScript. You can go line-by-line or block-by-block and discuss each part individually.

Mine is fun but harder to read (or maybe grasp) for the n00bs.:slight_smile:

1 Like

Hmm, just realized that the script (either version!) creates a new window, instead of switching the workspace used in the current window. So if you keep running it, you get window after window…

Can the “load workspace” command be targeted to the current window?

Can the “load workspace” command be targeted to the current window?

No. This is likely a bug in the command @cgrunenberg would need to assess.

Haha just one line. Nice.

Jim do you have a script list(your personal) somewhere that anyone can use?
You have been here for many years I’m sure you have a list somewhere lol

I know, I know, my scripts may not suit your workflow etc. But I’m always looking for scripts such as OP

Jim do you have a script list(your personal) somewhere that anyone can use?

You mean scripts I’ve written or use?

PS: The one-liner is a personal challenge to me, but not something I think everyone should strive for, especially in the beginning. The Achilles heel of one-liners is it doesn’t allow for error-trapping so the conditions have to be just right to pull it off.

Both written or use: I bet we all could learn a lot about scripting from digging into your collection. Even if they are all one-liners. :slight_smile:

Even if they are all one-liners.

Hahaha! They for sure aren’t all one-liners.

Many of my scripts are internal proofs-of-concept, explorations, or one-off solutions for customers. A quick search of my Coding folder yields 744 scripts. :open_mouth: :slight_smile:
(Note: Some of these would be what I refer to as scriptgrounds, test chunks of code or previous iterations before refactoring.)

Okay that is super handy and cool. (By the way, @BLUEFROG, I went ahead and bought Sal S.'s book 1-2-3 Applescript based on our discussions on this forum).

Question: how do I give an AppleScript in the Toolbar a nicer icon?

(By the way, @BLUEFROG, I went ahead and bought Sal S.'s book 1-2-3 Applescript based on our discussions on this forum)

Nice! Sal is the Man, for sure. He and Shane Stanely are giants in Mac automation.

(By the way, @BLUEFROG, I went ahead and bought Sal S.'s book 1-2-3 Applescript based on our discussions on this forum)
You can change the icon for the script in the Finder, but currently it will be bounded in a button. Like so…

image

@cgrunenberg would have to assess whether this can be changed to allow for just displaying the icon itself. (I’m guessing it would not be a simple change.)

That’s all I want, actually, thanks!

You’re welcome!

Did think to do that - awesome! Can finally hide the text in the toolbar.

Can finally hide the text in the toolbar.

Do note: Icon only will hide the document name and proxy icon.

A couple changes:
*improved the dialog for selection
*@ngan added code to close all open windows before loading the new ones

-- Load Workspace
-- from a list of all saved workspaces

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application id "DNtp"
	try
		set theWorkspaces to workspaces
		set mychoice to (choose from list theWorkspaces with prompt "Load this workspace" default items "None" OK button name {"Load"} cancel button name {"Cancel"})
		if mychoice is not false then
			set theSelection to item 1 of mychoice
			close every think window
			load workspace theSelection
		end if
	end try
end tell

This script can be paired with the following script, which will save your workspace to one of your saved ones. If you put these scripts in the tool bar, it makes it easy to load and save workspaces for individual projects, etc.

-- Save Current Workspace
-- to a list of all saved workspaces

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application id "DNtp"
	try
		set theWorkspaces to workspaces
		set mychoice to (choose from list theWorkspaces with prompt "Save this workspace to:" default items "None" OK button name {"Save"} cancel button name {"Cancel"})
		if mychoice is not false then
			set theSelection to item 1 of mychoice
			save workspace theSelection
		end if
	end try
end tell
3 Likes

I almost don’t dare to ask but how do you place a single script in the toolbar?

In this forum, u can ask for anything and very likely will be getting a response. FYI, I also didn’t know where to place a script and not even understand what a script is about 18 months ago.

For putting script under the toolbar menu, see pg 179-180 of the manual. Just remember to quit and re-open DT3 AFTER you place the script in the folder. The customisation of button is mostly similar to other app, ctrl-click the toolbar and you see a list of buttons for drag and drop on to the toolbar.

Also even if you aren’t on DevonThink, combined with bring DT front and your script, this script brings DT front then brings window to choose workspaces.


activate application "DEVONthink 3"
tell application "System Events"
	tell process "DEVONthink 3"
		try -- Symbols
			set focused of scroll area 1 of group 1 of splitter group 1 of splitter group 1 of window 1 to true
		end try
		try -- List
			set focused of outline 1 of scroll area 1 of group 1 of splitter group 1 of splitter group 1 of window 1 to true
		end try
		try -- Columns
			set focused of scroll area -2 of scroll area 1 of browser 1 of splitter group 1 of group 1 of splitter group 1 of splitter group 1 of window 1 to true
		end try
		try -- Two pane
			set focused of outline 1 of scroll area 1 of splitter group 1 of group 1 of splitter group 1 of splitter group 1 of window 1 to true
		end try
		try -- Three pane / Tags
			set focused of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of group 1 of splitter group 1 of splitter group 1 of window 1 to true
		end try
	end tell
end tell
tell application id "DNtp"
	try
		set theWorkspaces to workspaces
		set mychoice to (choose from list theWorkspaces with prompt "Load this workspace" default items "None" OK button name {"Load"} cancel button name {"Cancel"})
		if mychoice is not false then
			set theSelection to item 1 of mychoice
			close every think window
			load workspace theSelection
		end if
	end try
end tell