keyboard selection from Prompt Options dialog

Get help using and writing Nisus Writer Pro macros.
credneb
Posts: 142
Joined: 2007-03-28 07:30:34

keyboard selection from Prompt Options dialog

Post by credneb » 2012-01-30 09:57:48

I use Prompt Options to display a dialog box with a number of variable choices, selected by radio buttons. The macro and the dialog box work fine.

The problem is that sometimes -- and very rarely --I can use the up/down cursor arrows to cycle through and select the desired radio button. Being able to use the keyboard instead of the mouse is great and preferable to requiring the mouse. Unfortunately it is rarely possible.

Which is the expected behavior? Is there any way to control it, and if so, how?

Thanks.

Cliff Bender

User avatar
martin
Official Nisus Person
Posts: 4251
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: keyboard selection from Prompt Options dialog

Post by martin » 2012-01-30 15:55:12

There isn't really any way to control that behavior, but your desire to have the options navigable via the keyboard should always be the case. I just did some (very limited) tests and it seems to be that way. Perhaps you can share a macro where the options aren't keyboard navigable?

credneb
Posts: 142
Joined: 2007-03-28 07:30:34

Re: keyboard selection from Prompt Options dialog

Post by credneb » 2012-02-01 08:13:05

Thanks for checking the behavior. Here is the macro as used. I hope posting it btw the code tags is the way to do it.

Code: Select all

#Select Glossary
#Enables selecting the glossary to edit from a dialog box.
#The glossary is used only once and the automatically selected glossary
#reverts to the job glossary derived from the current job name.

Require Pro Version 1.2
$doc = Document.active
$hasDoc = Defined $doc
$noFile = ''
If ! $hasDoc
	$noFile = true
	#if no document is open, proceed to select glossary
else
	$path = $doc.filePath
	$hasPath = Defined $path
		#if a document is open but there is no path, it's Untitled
		#and has not been saved. 
	If ! $hasPath
		$save = Prompt 'Save the document before proceeding?', '', 'No', 'Save'
		if $save == "Save"
			Exit
		end
	End
end

	#get the name of the last glossary used; save the glossary used at the end
$registry = Registry.applicationRegistry
$lastSaved = 'com.j2e.glossaryMacros.lastGlossarySaved'
$lastUsed = $registry.loadValueWithName($lastSaved)
If $lastUsed == undefined
	$lastUsed = 'New Glossary.ngloss'
	$registry.saveValueWithName($lastUsed, $lastSaved)
end

	#extract the name of the glossary for the current job
	#filename format = jobnumber.title.rtf (or other extension)
if $noFile
	$currentJobGlossary = 'New Glossary.ngloss'
else
	$jobName = $doc.displayName
	$loc = $jobName.rangeOfString('.')
	if Defined($loc)
		$range = Range.new(0,$loc)
		$jobNumber = $jobName.subtextInRange($range)
		$currentJobGlossary = $jobNumber&'g.ngloss'
	else
		$currentJobGlossary = $lastUsed
	end
end

	#now get list of glossaries in glossary folder
$glossaryFolderPath = '~/Library/Application Support/Nisus Writer/Glossaries'
$glossariesInFolder = File.namesInFolderAtPath $glossaryFolderPath

	#strip backups marked with * from selection array
ForEach $index, $value in reversed $glossariesInFolder
	$delete = $value.find('*')
		If Defined($delete)
			$glossariesInFolder.removeValueAtIndex($index)
		End
End

	#discover if job glossary is in folder
$index = $glossariesInFolder.indexOfValue($currentJobGlossary)
If $index > 0
		#if found, move the glossary derived from document title
		#to the beginning of the array to make it the default
	$glossariesInFolder.removeValueAtIndex($index)
	$glossariesInFolder.prependValue $currentJobGlossary
end

If $lastUsed == 'directGlossary.ngloss'
		#This is set in call_SelectGlossary macro that is used to 
		#display the dialog box when wanted. 
		#Otherwise, you may have to click on or open a different document
		#to select a glossary at will. 
	$message = 'Select the glossary to edit.'
	$gloss = Prompt Options $message, '', 'Select', $glossariesInFolder
	if $gloss == "Default Option"
		#there are no glossaries in the glossary folder, so quit
		$input = Prompt 'There are no glossaries in the Glossary folder. Exiting.', '', 'OK'
		Exit
	end

	$glossaryPath = $glossaryFolderPath.filePathByAppendingComponent($gloss)
	$registry.saveValueWithName($gloss, $lastSaved)
else
	If $lastUsed == $currentJobGlossary
			#if the last glossary used was for the active document,
			#assume you want to edit that glossary, so open without
			#presenting selection dialog box. 
		$glossaryPath = $glossaryFolderPath.filePathByAppendingComponent($currentJobGlossary)
		$gloss = $currentJobGlossary
	else
			#otherwise, let the user select the glossary
		$message = 'Select the glossary to edit.'
		$gloss = Prompt Options $message, '', 'Select', $glossariesInFolder
		if $gloss == "Default Option"
			#there are no glossaries in the glossary folder, so quit
			$input = Prompt 'There are no glossaries in the Glossary folder. Exiting.', '', 'OK'
			Exit
		end
		$glossaryPath = $glossaryFolderPath.filePathByAppendingComponent($gloss)
		$registry.saveValueWithName($gloss, $lastSaved)
	end
end

	#is the glossary enabled?
$loc = $gloss.rangeOfString('.')
if Defined($loc)
	$range = Range.new(0,$loc)
	$testEnabled = $gloss.subtextInRange($range)
end
$isEnabled = Glossary Enabled $testEnabled
If ! $isEnabled
	$message = 'The glossary = '&$testEnabled
	$message = $message&' = is turned off. Turn ON?'
	$input = Prompt $message, '', 'No', 'Turn ON'
	If $input == "Turn ON"
		Set Glossary Enabled $testEnabled, true
	End
End

Open $glossaryPath




User avatar
martin
Official Nisus Person
Posts: 4251
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: keyboard selection from Prompt Options dialog

Post by martin » 2012-02-01 17:15:48

credneb wrote:I hope posting it btw the code tags is the way to do it.
That's a fine way to post a macro. Either that or as a forum file attachment are both good ways to share a macro.

Well, it turns out that I don't think this has anything to do with your macro itself. Instead I think it depends on whether or not you have "full keyboard access" turned on in your OSX system preferences. If you do, it seems the keyboard focus might start on the push buttons in the prompt, instead of on the options themselves. If you press the Tab key a few times you should be able to move the focus to the desired radio/option button area, so you can use the arrowkeys to navigate them.

Let me know if that explains this phenomenon for you. If so, I can file a change request that we make sure to focus the radio/option button area regardless.

credneb
Posts: 142
Joined: 2007-03-28 07:30:34

Re: keyboard selection from Prompt Options dialog

Post by credneb » 2012-02-03 07:17:39

re: full keyboard access on/off

That seems to be it. Mine was set to input and text areas only, so no amount of tabbing helped, but changing the setting to all controls (or whatever it is labelled in English) let me tab around btw the radio buttons and the stop macro and OK buttons.

I have not tested it thoroughly, but this also seems to be why cmd-period sometimes does, sometimes doesn't work to cancel a macro from a dialog box.

Thank you for filing a change request to put the focus on the radio buttons.

Þorvarður
Posts: 239
Joined: 2012-12-19 05:02:52

Re: keyboard selection from Prompt Options dialog

Post by Þorvarður » 2014-07-16 01:06:02

This is an interesting macro, and I would like to ask a few questions. I don't fully understand how it works yet, so please bear with me.

1) My glossary file path is '~/Library/Application Support/Nisus Writer Pro/Glossaries' and NOT '~/Library/Application Support/Nisus Writer/Glossaries'. Does this mean that I accidentally changed the name of the folder "Nisus Writer" to "Nisus Writer Pro" some time in the past without actually knowing what I was doing?

2) The prompted message 'Select the glossary to edit.' shows me only 7 "glossaries", and some are actually not glossaries but folders. I have 26 glossaries. As I now open my glossary folder in Finder, I see that I have grouped my glossaries into several folders according to language, projects, and field of knowledge. This means that my 26 glossaries reside in 7 hierarchical folders within the all embracing Glossaries folder.

(A) Is it possible to rewrite the macro so that all glossaries will be shown in the message pane?
(B) According to the macro, you seem to have all glossaries in one single folder. Is that the preferred way Nisus users choose to have it?


Thanks.

User avatar
phspaelti
Posts: 894
Joined: 2007-02-07 00:58:12
Location: Japan

Re: keyboard selection from Prompt Options dialog

Post by phspaelti » 2014-07-16 08:07:28

Þorvarður wrote: 1) My glossary file path is '~/Library/Application Support/Nisus Writer Pro/Glossaries' and NOT '~/Library/Application Support/Nisus Writer/Glossaries'. Does this mean that I accidentally changed the name of the folder "Nisus Writer" to "Nisus Writer Pro" some time in the past without actually knowing what I was doing?
My application folder is also called "Nisus Writer Pro". There may be some history here.
Þorvarður wrote: 2) The prompted message 'Select the glossary to edit.' shows me only 7 "glossaries", and some are actually not glossaries but folders. I have 26 glossaries. As I now open my glossary folder in Finder, I see that I have grouped my glossaries into several folders according to language, projects, and field of knowledge. This means that my 26 glossaries reside in 7 hierarchical folders within the all embracing Glossaries folder.

(A) Is it possible to rewrite the macro so that all glossaries will be shown in the message pane?
Yes, but you basically have to do it yourself, that is you have to check each file path for subfolders and so on. If you only have one level of folders in the glossary folder, code like the following would fix it:

Code: Select all

$temp = Array.new
foreach $value in $glossariesInFolder
	if File.isFolderAtPath "$glossaryFolderPath/$value"
		$glNames = File.namesInFolderAtPath("$glossaryFolderPath/$value")
		$temp.appendValuesFromArray $glNames
	else
		$temp.push $value
	end
end
$glossariesInFolder = $temp
Insert the above code after the line:

Code: Select all

$glossariesInFolder = File.namesInFolderAtPath $glossaryFolderPath
Here is my version with the added fix:

[The extension nwm has been deactivated and can no longer be displayed.]

philip

credneb
Posts: 142
Joined: 2007-03-28 07:30:34

Re: keyboard selection from Prompt Options dialog

Post by credneb » 2014-07-16 08:42:42

Hi, Þorvarður.

And praise be the copy/paste function! May I ask how you pronounce your name?

1) Glossary file path: I actually just discovered that my file path is also .../Nisus Writer Pro/, and NWP must be doing some magic in the background because it puts my glossaries in the ...Pro... folder. It seems to work either way.

2) I don't know the answer. My glossary folder currently has 12 glossaries in it, and they all show up in the selection dialog box. If there are 20 glossaries, they all show up.

The macro simply gets a list of what is in the glossary folder. More correctly I think this is called a a directory (or subdirectory). I'm guessing there is a total of 7 entries in your glossary folder, some of which are not glossary files but folders (subdirectories), and the macro simply returns a list of those 7 entries, including the folders.

(A) I'm sure the macro could be changed to drill down. Sounds like an interesting learning exercise, and I will try to do it.
(B) All glossaries in one folder is simply the way I have them organized.

A simple set of three related macros, including an updated version of the select glossary macro, is attached. These macros came from my needs translating patents from Japanese to English. Allow me to explain my work flow to help understand how these macros work.

Each job file starts with a unique number. When I start a new job, a different macro creates and saves a new file from a template using the name of the source document in the job folder, and creates a new glossary file with the same file number followed by a "g" and puts it in the glossary folder. That glossary file always goes to the top of the list if the macro is called from the job file (that makes it the default selection).

To create a new glossary entry for a particular job, I select the text to be the expansion and hit cmd-K (which is assigned to the Single Entry macro included). That macro extracts what I usually use as the abbreviation and presents a dialog to change it as desired. Hitting Return continues the macro, which calls Select Glossary.

Depending on the conditions commented in the Select Glossary macro, the job glossary is automatically opened, the new entry is appended, and the glossary automatically saved. So at the shortest, I can create a new entry and get back to work in seconds by (1) selecting the expansion text, (2) hitting cmd-K, (3) hitting Return, and (4) hitting cmd-W.

My macro gives me the chance to turn a glossary ON if I select one that is OFF from my dialog. I only use the NWP preference pane to turn glossaries off (because I have not gotten around to adding this function).

Variations on the Single Entry macro create multiple entries in batches from different types of lists.

Because Select Glossary is called from other macros, I use the macro call_SelectGlossary to invoke the SG macro.

If you find these useful, I'd be happy to share others in the set.

Hope this helps.

Cliff
Attachments
single glossary entry set.zip
(14.21 KiB) Downloaded 132 times

User avatar
martin
Official Nisus Person
Posts: 4251
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: keyboard selection from Prompt Options dialog

Post by martin » 2014-07-16 12:19:42

Þorvarður wrote:Does this mean that I accidentally changed the name of the folder "Nisus Writer" to "Nisus Writer Pro" some time in the past without actually knowing what I was doing?
Do not be concerned about the folder name. It used to be just "Nisus Writer", but later was changed to "Nisus Writer Pro" as per Apple's guidelines for naming application support folders. They prefer that the folder name exactly match the application name.

User avatar
phspaelti
Posts: 894
Joined: 2007-02-07 00:58:12
Location: Japan

Re: keyboard selection from Prompt Options dialog

Post by phspaelti » 2014-07-16 16:04:17

credneb wrote:(A) I'm sure the macro could be changed to drill down. Sounds like an interesting learning exercise, and I will try to do it.
Here is a general purpose approach to this problem. This macro will get all nested files from a chosen folder.

Code: Select all

# Get All Nested Files in Folder

# Helper function to get filepaths inside a folder
Define Command GetPathsInFolder($folder)
	$pathList = Array.new
	$fileNames = File.namesInFolderAtPath $folder
	foreach $name in $fileNames
		$file = $folder.filePathByAppendingComponent $name
		$pathList.push $file
	end
	return $pathList
end

# Prompt user for a starting folder
$folder = Choose Folder

# Prepare a stack of filepaths to handle recursion
$files = $stack = Array.new
$pathList = GetPathsInFolder($folder)
$stack.appendValuesFromArray $pathList

# Recursively check each filepath for nested files
# and collect all filepaths
while $stack.count
	$path = $stack.dequeue
	if File.isFolderAtPath($path)
		$pathList = GetPathsInFolder($path)
		$stack.appendValuesFromArray $pathList
	else
		$files.push $path
	end
end

# Output filepaths as a list
Document.newWithText $files.join("\n")
And here the macro file for the above code:

[The extension nwm has been deactivated and can no longer be displayed.]

philip

Þorvarður
Posts: 239
Joined: 2012-12-19 05:02:52

Re: keyboard selection from Prompt Options dialog

Post by Þorvarður » 2014-07-18 00:54:17

Hi Cliff,
credneb wrote:May I ask how you pronounce your name?
https://copy.com/qERpbFW8JVhj
(A) I'm sure the macro could be changed to drill down. Sounds like an interesting learning exercise
Yes, for me this is also an interesting learning exercise.:-)
A simple set of three related macros, including an updated version of the select glossary macro, is attached.
Thank you very much, Cliff.
Each job file starts with a unique number.
How do you select that unique number? Is that arbitrary? Or do you have a certain system, for example one that determines how the file will be sorted in the job folder?
To create a new glossary entry for a particular job, I select the text to be the expansion and hit cmd-K (which is assigned to the Single Entry macro included). That macro extracts what I usually use as the abbreviation and presents a dialog to change it as desired. Hitting Return continues the macro, which calls Select Glossary.
Depending on the conditions commented in the Select Glossary macro, the job glossary is automatically opened, the new entry is appended, and the glossary automatically saved. So at the shortest, I can create a new entry and get back to work in seconds by (1) selecting the expansion text, (2) hitting cmd-K, (3) hitting Return, and (4) hitting cmd-W.
I tried to follow your steps, and I must say, I think this can be a huge time-saver; and last but not least, it's fun to work like this.

My problem at the moment is, that my glossaries are organized in subfolders, and they show up wrongly as files in the macro (see my answer to Philip Spaelti).
My macro gives me the chance to turn a glossary ON if I select one that is OFF from my dialog. I only use the NWP preference pane to turn glossaries off (because I have not gotten around to adding this function).
Maybe somebody else will be able to figure out how to do that and will be able to share that missing link.
Variations on the Single Entry macro create multiple entries in batches from different types of lists. (…) If you find these useful, I'd be happy to share others in the set.
Yes by all means, that would be great.

Thanks again for sharing.

User avatar
phspaelti
Posts: 894
Joined: 2007-02-07 00:58:12
Location: Japan

Re: keyboard selection from Prompt Options dialog

Post by phspaelti » 2014-07-18 01:17:51

Þorvarður wrote:My problem at the moment is, that my glossaries are organized in subfolders, and they show up wrongly as files in the macro (see my answer to Philip Spaelti).
Did I miss this answer? Did my fix for this problem work for you? Let me know if there are any problems.
philip

Þorvarður
Posts: 239
Joined: 2012-12-19 05:02:52

Re: keyboard selection from Prompt Options dialog

Post by Þorvarður » 2014-07-18 01:32:22

Sorry Philip,

no you didn't miss anything. I'm struggling with the Nisus forum message window while trying to include screenshots. I keep getting this message: "Internal Server Error. The server encountered an internal error or misconfiguration and was unable to complete your request." I think I have finally succeeded now. :-)

So here comes the message…
phspaelti wrote:My application folder is also called "Nisus Writer Pro".
Thanks for the confirmation.
Here is my version with the added fix:
This is clearly an improvement. I wonder if it would be possible to somehow track all the .ngloss files in the Glossaries folder, including the ones in subfolders, but excluding files that do not have the ending .ngloss?

My Glossaries folder has subfolders and a few files which are not .ngloss files. These files are thematically related to glossaries, therefore I keep them there.

Here is a screenshot of the Glossaries folder: Oops, I get an error message now: "The file is too big, maximum allowed size is 100 KiB."

The screenshot is now here: https://copy.com/tGnvFnUPD3Ia

And here we see how the macro works (i.e. the selection dialog box): Red shows folders, and green shows a picture file and a RTF file.

https://copy.com/o8utRx7QHhzA

User avatar
phspaelti
Posts: 894
Joined: 2007-02-07 00:58:12
Location: Japan

Re: keyboard selection from Prompt Options dialog

Post by phspaelti » 2014-07-18 01:40:12

Þorvarður wrote:
phspaelti wrote:Here is my version with the added fix:
This is clearly an improvement. I wonder if it would be possible to somehow track all the .ngloss files in the Glossaries folder, including the ones in subfolders, but excluding files that do not have the ending .ngloss?

My Glossaries folder has subfolders and a few files which are not .ngloss files. These files are thematically related to glossaries, therefore I keep them there.
Okay, I see now.
Well then, it seems you are going to need the recursive version. And of course the bit about excluding other types of files can be added too.
philip

User avatar
phspaelti
Posts: 894
Joined: 2007-02-07 00:58:12
Location: Japan

Re: keyboard selection from Prompt Options dialog

Post by phspaelti » 2014-07-18 02:23:54

So here now is my attempt to merge the two macros: Cliff's original and my nested file retriever.
The result is not ideal Cliff's original macro works only with the file names, not paths. But if you are working with nested files, the names of different files can be identical, as long as they are in different folders. So one should really rewrite the macro to work with paths, rather than file names. But since Cliff's macro has many other bits specific to his workflow, one will probably have to make some decisions on how to adjust this.

[The extension nwm has been deactivated and can no longer be displayed.]

PS: As I suspected. The current macro will interact badly with Cliff's automatic name/glossary creation, and there is no simple solution, because once you have nested glossaries, the user will have to decide in which folder or sub-folder to save the glossary. Maybe if you only need the part about opening/turning on glossaries, the macro should be stream-lined.
philip

Post Reply