regular expression in a file name

Get help using and writing Nisus Writer Pro macros.
Post Reply
js
Posts: 259
Joined: 2007-04-12 14:59:36

regular expression in a file name

Post by js »

I wrote a macro that uses the Open command, like this:

Open "~/Documents/myFolder/myFile"

Now if I don‘t know the complete name of myFile, but just let‘s say the beginning, is it possible to open it anyway, using a regular expression? (This seems to work with command lines in the terminal).
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Post by martin »

This is possible, but will require Perl's "glob" operator, which returns an array of matching file names. An example which opens the first text file in the user's Documents folder:

Code: Select all

$openFile = ''

Begin Perl
	@paths = glob('~/Documents/*.txt');
	$openFile = $paths[0];
End

Open $openFile
js
Posts: 259
Joined: 2007-04-12 14:59:36

Post by js »

martin wrote:This is possible, but will require Perl's "glob" operator, which returns an array of matching file names. An example which opens the first text file in the user's Documents folder:

Code: Select all

$openFile = ''

Begin Perl
	@paths = glob('~/Documents/*.txt');
	$openFile = $paths[0];
End

Open $openFile
Thanks. This was very helpful. Just one point: When I simply copied your code and pasted it into a nisus macro document, it would not execute. Only after applying Formats:Remove Attributes AND removing the empty space in the 2 lines after "Begin Perl" it worked. I am somewhat astonished because once the macro worked, I tried to add attributes like color and bold and it still executed fine. How come?
User avatar
Hamid
Posts: 777
Joined: 2007-01-17 03:25:42

regular expression in a file name

Post by Hamid »

If you use Firefox and copy the code, the code will execute as it is.
You must have used Safari, I presume; it added spaces, including 2 no-break spaces and a Control character to the code. Removing attributes was not necessary.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Post by martin »

Hamid has got it- Perl doesn't seem to like the non-breaking space (or probably any other "high" UTF-8 characters) outside of string literals. I'll see if there's anything we can do.
js
Posts: 259
Joined: 2007-04-12 14:59:36

Re: regular expression in a file name

Post by js »

Hamid wrote:

Code: Select all

$openFile = ''

Begin Perl
	@paths = glob('~/Documents/*.txt');
	$openFile = $paths[0];
End

Open $openFile
If you use Firefox and copy the code, the code will execute as it is.
You must have used Safari, I presume; it added spaces, including 2 no-break spaces and a Control character to the code. Removing attributes was not necessary.
Thanks for your and everybody's help. I should insist though that removing attribute is necessary. (In fact this is why I don't use Firefox: because it is not able to get text attributes on a clipboard, for me a top priority in a Browser, but in this case the source of a little problem).

But here is a sequel to my earlier query. While Martin's script works well, what I really want to do is not to find the first file of a folder (here don with $paths[0]), but one of which I know only the beginning of the name. Of course I could manually enter it, like

Code: Select all

@paths = glob('~/Documents/BEGOFFILENAME*.txt');
But what I finally want is to use a selection of text for that. So I tried to start my Nisus macro with
$BEGOFFILENAME = Read Selection
and then substituting this into the Perl macro, like

Code: Select all

@paths = glob('~/Documents/$BEGOFFILENAME*.txt');
But this does not work. It seems that Perl cannot evaluate the scalar value within the expression. Is there a solution to this?
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Post by martin »

Perl will only interpolate variables in a string literal if you use double quote marks, eg:

Code: Select all

@paths = glob("~/Documents/$filePath*.txt");
js
Posts: 259
Joined: 2007-04-12 14:59:36

Post by js »

martin wrote:Perl will only interpolate variables in a string literal if you use double quote marks, eg:

Code: Select all

@paths = glob("~/Documents/$filePath*.txt");
Thanks. Finally my macro seems to work. Nearly. First I found problems with empty space in pathnames and or input and how this can be solved. Llke a pathname 'My file' has to be coded as "My\\ file". But how to deal with filenames that use letters in the higher ASCII range, like é or ö?
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Post by martin »

This looks like a problem with the character encoding. It appears that the glob function doesn't like to handle UTF-8 strings. I'll have to look around for a solution to this.
Post Reply