Need help with a Wonky Applescript for a very hacky hack (think view columns)

OK. What I want to do: Create a script for showing different columns in the think view.
Basically, using System Preferences shortcuts to create several Option-Command-Shift key combinations, then using Applescript’s System Events Keystroke or Key Code "using {foo down, bar down}

I created the shortcuts (e.g., command-option-shift-M for the Devonthink menu item “Modified Date”)

They worked normally.

Then I figured I could create an AS that uses a repeat routine to tell System Events to press these keys one after the other.

When run (as a script from the DT toolbar) it causes the frontmost DT window to disappear, take a few seconds, reappear, and no (new) columns were shown.

My AS skills are super bad, of course, so I suspect I may be either 1. Doing something very stupid and/or 2. Abusing the system very severely :slight_smile:

Any comments appreciated! - script is below:

tell application id "DNtp"
	
	set theKeyCodes to {"S", "B", "T", "K", "M"}
	
	repeat with aCode from 1 to length of theKeyCodes
		
		tell application "System Events" to keystroke aCode using {option down, shift down, command down}
		
		delay 3
		
	end repeat
	
end tell
1 Like

Knowing what these shortcuts do would be useful to comprehend the script.

Hi Christian, sorry for the long post, but I started it by saying what I wanted to do- show columns in the list/table view of the think window.
I created Mac shortcuts for, e.g view column “Date Modified” as cmd-opt-shift-M. And then I tried pressing that and other sets of key combinations in succession in the script.

I don’t think you are doing what you actually want to;

In my reading, this

repeat with aCode from 1 to length of theKeyCodes

should lead to aCode taking on values from 1 to 5; you are then sending those values as keystrokes (with the modifiers shift, option, and command). I presume what you actually want to do is to send the letters making up theKeyCodes.

I haven’t tried it, but I would assume the following modification should work:

tell application id "DNtp"
	set theKeyCodes to {"S", "B", "T", "K", "M"}
	repeat with aCode from 1 to length of theKeyCodes
		tell application "System Events" to keystroke (item aCode of theKeyCodes) using {option down, shift down, command down}		
		delay 3		
	end repeat	
end tell

This can also be expressed as count KeyCodes.

1 Like

Blanc, many thanks, worked exactly as I wanted!

1 Like

tx Jim!

1 Like

Been happy with my new toolbar script, saves me many trips to the “view columns” menu :slight_smile:

2 Likes