Opening Documents By Double-clicking

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
teks
Posts: 7
Joined: Thu Dec 18, 2008 1:31 am

Opening Documents By Double-clicking

Post by teks » Fri Dec 19, 2008 5:14 am

Hi all,

I am trying to put together a standalone app that will be able to save 2 (or more) different types of documents - neither of which will be in native rev stack format.

I have different scripts setup to open each type of document, and they work fine when the application is launched, and the user selects the document via an 'Open' dialogue box. However, what am I supposed to do if the user double-clicks one of the documents in their file system?

I have read the (really great) documentation provided, and have managed to successfully map file extensions/registry keys, etc., of the appropriate document types to my application. Double-clicking a document of the appropriate type DOES launch my application. And nothing else. Now, my application needs to somehow find out the name/path of the clicked document, so that it can run its appropriate document-opening script.

When the user double-clicks the document in the file system, the system launches the application, and - I'm certain - at the system level, passes the name/path of the clicked document to the application being opened. I cannot, however, find anywhere in the Revolution documentation a way to get that, so that I can automatically invoke the appropriate 'open' command.

I would have expected that some of the Revolution messages sent to the main stack would provide an easy mechanism to access the path of the document that was clicked - ie., as a parameter argument to the 'startup' message, the 'resumeStack' message, or even perhaps a specially-designed message (something like 'openFile pFileList'). This, however, does not seem to be the case.

After searching the documentation and the forum, all I could find was this old thread:

http://forums.runrev.com/phpBB2/viewtopic.php?t=559

This, of course, seem very much to be a patch-up solution - and which would only work on the Macintosh, as it depends on calls to AppleScript.

Please help!!!

I am truly hoping that someone is going to say, that I have missed something obvious!... :?

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Fri Dec 19, 2008 7:06 am

On Mac, you'll receive an 'AppleEvent' that you can trap as follows

Code: Select all

on appleEvent pEventClass, pEventId
  -- both the class and id are four-character codes
  -- I prefer to combine them so I can use a single 'switch'
  put pEventClass & pEventId into tLongType
  switch tLongType
  case "aevtodoc"  -- a document was opened from the finder
    -- fetch the file path
    request appleevent data
    put it into tFilePath
    -- now call our file opening routine
    MyOpenFile tFilePath
    break
  -- insert other cases here for other events
  end switch
  pass appleEvent
end appleEvent
I tend to place such a script in the stack script of my project mainstack.

On Windows and Linux, the filenames will be in the argument variables $0 through $9. So there you'll typically handle it once you've loaded all your libraries and are ready to edit files.

Code: Select all

repeat with i = 1 to $#
     put value("$" & i) into tFilePath
     MyOpenFile tFilePath
  end repeat
Note that $# requires Revolution 2.9 or higher.

HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

teks
Posts: 7
Joined: Thu Dec 18, 2008 1:31 am

Post by teks » Fri Dec 19, 2008 7:17 am

Jan, thank you very much for the fantastic advice, which will save me many hours of hair-pulling! :)

A couple of questions, if I may:

1) On which message handlers do you recommend these should be placed? Would it be:
* in a 'startup' handler - to catch the case where the user is double-clicking a document, and the application has not been launched yet
* also in a 'resumeStack' handler, to catch the situation where the user is double-clicking a document, and the application is already running

2) How do you find out about things like "$#"??? :o

Many thanks in advance!

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Fri Dec 19, 2008 11:37 am

I'm glad to be of assistance. The situation for Mac is fairly easy, but the Windows/Linux side is a bit more involved.

Standard behaviour on Mac is that the operating system sends the appleEvent message to your standalone. But Windows and Linux will open a new instance of your application.

Thankfully, the Revolution team added the relaunch message in version 2.7.3 - which allows you to handle this situation by opening the files in the running instance, and telling the other instance to shut down, before anything is displayed on screen.

As for where to handle the opening of files on the Windows/Linux side, I handle that in an openStack handler, as this ensures I can use all externals (like the database and xml externals) - which aren't loaded and available yet when the startup message is sent.

So your project mainstack script could look something like this:

Code: Select all

on appleEvent pEventClass, pEventId 
  -- both the class and id are four-character codes 
  -- I prefer to combine them so I can use a single 'switch' 
  put pEventClass & pEventId into tLongType 
  switch tLongType 
  case "aevtodoc"  -- a document was opened from the finder 
    -- fetch the file path 
    request appleevent data 
    put it into tFilePath 
    -- now call our file opening routine 
    MyOpenFile tFilePath 
    break 
  -- insert other cases here for other events 
  end switch 
  pass appleEvent 
end appleEvent

on openStack
  -- avoid the file opening check in case a substack opens
  if the long ID of the target is not the long ID of me 
  then pass openStack
  -- check if we need to open any files
  if the platform is not "MacOS" then
    repeat with i = 1 to $# 
      put value("$" & i) into tFilePath 
      MyOpenFile tFilePath 
    end repeat
  end if
end openStack

on relaunch
  -- the command line arguments are passed as parameters
  -- note that this message will not be sent on MacOS
  repeat with i = 1 to the paramCount
    put param(i) into tFilePath
    MyOpenFile tFilePath
  end repeat
end relaunch
The above should just about cover all situations - though admittedly I haven't tested it on Linux and the docs for relaunch don't mention Linux support, but that could just be an outdated doc entry.

As for $# - it looks like it's missing from the dictionary, but I must have picked it up from the release notes of Revolution 2.9 at some point.

HTH,

Jan Schenkel.
Last edited by Janschenkel on Sat Dec 20, 2008 8:18 am, edited 1 time in total.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

teks
Posts: 7
Joined: Thu Dec 18, 2008 1:31 am

Post by teks » Fri Dec 19, 2008 10:29 pm

Jan, thank you so very, very much for your help. It would have certainly taken me many, many days of frustrating testing and research to try and overcome these problems on my own. Your guidance has been absolutely invaluable.

I have posted a bug/feature request on the Quality Control Centre, in regards to this - ie., this really should be easier to do, specially for newbies.:wink:


Many, many thanks.

nomodes
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7
Joined: Tue Nov 09, 2010 7:48 pm

Re: Opening Documents By Double-clicking

Post by nomodes » Fri Nov 30, 2012 7:25 pm

Jan's solution is excellent.

I wanted it to support an additional situation: On Mac OS, the user can select multiple documents and choose File > Open. The effect is as if he double-clicked them all, one at a time. This one change to the appleEvent handler takes care of that situation:

Code: Select all

  case "aevtodoc"  -- one or more documents were opened from the finder 
    -- fetch the file path 
    request appleevent data 
    put it into tFilePaths
    repeat for each line tFilePath in tFilePaths
        -- now call our file opening routine 
        MyOpenFile tFilePath
    end repeat
    break 

nomodes
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7
Joined: Tue Nov 09, 2010 7:48 pm

Re: Opening Documents By Double-clicking

Post by nomodes » Tue Nov 26, 2013 5:46 am

I recently used the solution I posted Nov 30, 2012, shown just above, derived from Jan's excellent post. But the switch statement in my handler was followed, unconditionally, by a "pass" statement. That made LiveCode send the "appleEvent" message further up the message path. And that caused lots of problems, the least of which was the appearance of duplicate entries in the Windows menu.

All the problems went away when I tweaked my handler to bypass the "pass" when it had fully handled the "appleEvent" message. A similar solution is shown in another board.

Post Reply

Return to “Talking LiveCode”