Passing a file to a compiled script?

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Passing a file to a compiled script?

Post by Phil Pendlebury » Tue Feb 02, 2010 2:48 pm

I have looked at all the topics and they don't seem to apply to what I want (I'm not even sure if it's possible - although I get the feeling it probably is).

As an example (this is part of a pretty massive script):

Say I have a script that when compiled is called parameter_test.exe

First all I want to do is get the name of a file dragged onto that script.

So for example I drop the image:

C:\Users\Phil\Downloads\test.jpg

On to the parameter_test.exe

The script now opens and I (for example) want it to simply display a message with the image name. C:\Users\Phil\Downloads\test.jpg

(The actual script is not that simple but this is the easiest way I can think of to explain what I need).

I will also then need to test if the variable that has been defined by opening the exe with a drag and drop has already been assigned or not. So that if the exe is started by a normal double click a different procedure will happen.

I hope this makes sense and I hope someone can offer some assistance.

So can someone show the code (if it is possible) required to show a message of the filename that has been dragged onto a compiled macro exe file.

And if that is possible - the code required to check if that variable has been assigned (although I suspect this is just a permutation of if>exists).

If I can get this working I will post the full script as it is quite useful and shows off some of MS great features.

Thanks.
Phil Pendlebury - Linktree

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Feb 02, 2010 3:06 pm

'fraid that's not possible. An exe has no drag-drop support like that.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Tue Feb 02, 2010 3:08 pm

Ahh Ok Marcus thank you. I thought I could see people doing something similar but I guess I misunderstood:-)

I'll post the script up anyway then shortly.
Phil Pendlebury - Linktree

User avatar
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Feb 02, 2010 3:16 pm

Sorry but I disagree.

Hi Phil,

The secret to this is the system variable command_line

Compile a short script with one line

MDL>%command_line%

Drop your jpg file on the executable and look at the result.
It will look some thing Like:
MDL> wrote: "C:\Documents and Settings\dickl\Desktop\Passing parameters.exe" "C:\Documents and Settings\dickl\Desktop\sample.JPG"
So within your script you need to:
a) recognize that a file was dropped on the executable
b) separate the filename from the command_line variable
c) deal with the passed file

a) What I do at the start of the script is separate command_line by "quote space quote" ( " " ) then look to see if I have two variables. If there are two then I have a passed parameter.

b) already done in a)

c) that's up to you.

Hope this is helpful.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Feb 02, 2010 3:22 pm

Doh - of course! I had tried it with MACRO_PARMS. Yes, JRL is right, it's inherent in Windows. When you drag something to the exe it is as if the filename was passed on the command line. So if you look at COMMAND_LINE you will see it in there and can parse it out. Sorry.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Tue Feb 02, 2010 3:38 pm

Super Mega Large! :-)

Thanks guys.
Phil Pendlebury - Linktree

User avatar
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Feb 02, 2010 3:53 pm

Here's a useful example. Compile the following then drop any file on the new executable's icon. The dropped file's name will be placed on the clipboard.

HERE is a precompiled executable called "Acquire File Name.exe" for anyone who wants it.

Otherwise here is the script to compile yourself.

Code: Select all

Separate>command_line," ",var
If>var_count=2
  StringReplace>var_2,",,var_2
  PutClipBoard>%var_2%
  MDL>var_2
Else
  Let>CommandLine=%Command_Line%
  StringReplace>commandline,",,commandline
  PutClipBoard>%commandline%
  MDL>Commandline
EndIf

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Tue Feb 02, 2010 4:36 pm

Thanks guys. This works for me:

Code: Select all

Separate>command_line," ,cmd_var
IF>cmd_var_2=
 Mdl>NO STRING
 // Do stuff you would do if app was started normally
ELSE
 Mdl>cmd_var_2
 // cmd_var_2 will be the file name
ENDIF

For some reason the " " in JRL's separate line was throwing me off.

:-)
Phil Pendlebury - Linktree

User avatar
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Feb 02, 2010 4:45 pm

For some reason the " " in JRL's separate line was throwing me off.
Hmmmm. All ( " " ) does is divide the called executable file from the file that is it's parameter. I'm using Win XP, I wonder if this displays differently in other OSs.

Does the scropt sample I posted above work for you?

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Tue Feb 02, 2010 4:45 pm

The finished section inserted into the script before the dialog and the ini file etc.

Code: Select all

Let>def_file=

Separate>command_line," ,cmd_var
IF>cmd_var_2=
 // DO NOTHING but leave this here just in case needed later
ELSE
 Let>def_file=cmd_var_2
 StringReplace>def_file,",,def_file
ENDIF
Not as elegant as JRL's but it works for me. :-)
Phil Pendlebury - Linktree

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Tue Feb 02, 2010 5:01 pm

Hi JRL.

It didn't seem to work constantly.

But I have a habit of doing things my own way - so that I understand them.

As you can see I still had to strip the " after inserting into my script. So that's an odd one.

I have to run now but will look at this later.

Your help was awesome as usual thank you both.

:)
Phil Pendlebury - Linktree

User avatar
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Oct 09, 2012 3:15 pm

I rewrote this to be able to process multiple files dragged onto the executable icon. Its a slightly different method to acquire the file name(s). I think its a little easier to follow. To use this just plug into the Process subroutine, the activity you want to perform on each file.

Code: Select all

LowerCase>Command_Line,Command_Line
LowerCase>script_file,script_file
StringReplace>Command_Line,",,Command_line
StringReplace>Command_Line,Script_File,,File[s]ToProcess

MidStr>File[s]ToProcess,1,3,drive
Separate>File[s]ToProcess,drive,file

Let>kk=1
Repeat>kk
  Add>kk,1
  Let>value=file_%kk%
  IfFileExists>value
    GoSub>Process
  EndIf
Until>kk=File_count

SRT>Process
  MDL>File %kk% = %value%
END>Process

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts