Reply to topic  [ 9 posts ] 
regular expression in a file name 
Author Message

Joined: 2007-04-12 14:59:36
Posts: 192
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).


2007-04-27 02:41:10
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 3637
Location: San Diego, CA
Post 
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:
$openFile = ''

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

Open $openFile


2007-04-27 12:45:08
Profile WWW

Joined: 2007-04-12 14:59:36
Posts: 192
Post 
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:
$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?


2007-04-28 01:58:10
Profile
User avatar

Joined: 2007-01-17 03:25:42
Posts: 593
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.


2007-04-30 03:46:55
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 3637
Location: San Diego, CA
Post 
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.


2007-04-30 15:11:12
Profile WWW

Joined: 2007-04-12 14:59:36
Posts: 192
Hamid wrote:

Code:
$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:
@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:
@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?


2007-05-01 04:45:26
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 3637
Location: San Diego, CA
Post 
Perl will only interpolate variables in a string literal if you use double quote marks, eg:
Code:
@paths = glob("~/Documents/$filePath*.txt");


2007-05-01 12:55:52
Profile WWW

Joined: 2007-04-12 14:59:36
Posts: 192
Post 
martin wrote:
Perl will only interpolate variables in a string literal if you use double quote marks, eg:
Code:
@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 ö?


2007-05-02 10:33:55
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 3637
Location: San Diego, CA
Post 
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.


2007-05-02 14:07:40
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software