Page 1 of 1

Open a file via Link

Posted: 2010-01-27 01:34:07
by loulesko
I have a nifty little macro that gets a file path and then prepends "file://" to the file path and inserts a link into my document which is excellent for referring to external documents.

When I click on the link it opens the folder and selects the document that the link refers to - all I have to do his Command-O to open the document. But, is there a way to have the macro open the target document for me even though the target document may be a PDF or jpg?

If not no big deal, I'm thrilled with what I have. But it doesn't hurt to ask.

Thanks
Lou

Re: Open a file via Link

Posted: 2010-01-27 03:15:33
by Kino
Put the insertion point somewhere in a hyper-link, e.g. “file:///Users/me/Documents/mylife.pdf”, and run this macro.

Code: Select all

$sel = TextSelection.active
$attr = $sel.text.displayAttributesAtIndex $sel.location
if $attr.link != undefined
	Open URL $attr.link
end
I just noticed an oddity in Open URL command. It encodes a url even if it is already encoded. For example, ‘%20’ in a link will be transformed to ‘%2520’.

Re: Open a file via Link

Posted: 2010-01-27 09:17:39
by loulesko
As always, thank yu very much Kino.

Lou

Re: Open a file via Link

Posted: 2010-08-30 22:07:02
by ssampler
Please post your macro, Lou.

Steve

Re: Open a file via Link

Posted: 2010-08-31 08:26:17
by loulesko
Here ya go Steve. I still haven't solved the problem of getting the target file to open. If anyone would like to improve on what I've done, please do. I'm not a programmer so there are probably better ways to approach this.

all the best
Lou

Code: Select all


#NisusMacroProperty Name: File Link
Require Application Version '3.1'
$filepath = User Property “home folder path”
$foldername = User Property “home folder name”
$filepath = "$filepath/$foldername"
$filepath = Choose File $filepath, ‘Select’
if $filepath != undefined
	$url = “file://“ & $filepath
	$displaytext = Prompt Input ‘Display text for:’, $filepath, ‘OK’
	if !$displaytext
	$displaytext = $url
	else
	$displaytext = $displaytext & “ ”
	end
	Type Link $url, $displaytext
end


Re: Open a file via Link

Posted: 2010-08-31 08:40:01
by martin
Thanks for sharing your macro Lou. I was just about to post my own version of your macro:

Code: Select all

$path = Choose File '', 'Link To'
If $path
	$link = "file://$path"
	$name = $path.lastFilePathComponentWithoutExtension
	Type Link $link, $name
End
Mine doesn't set a default folder path (it lets OSX decide where the file open panel should initially point), and it doesn't ask the user for the link's display text, it merely assumes the file name is good enough.

One tip on your code:
$filepath = User Property “home folder path”
$foldername = User Property “home folder name”
$filepath = "$filepath/$foldername"
You don't need to combine the two; the first "filepath" is already a full path to your user home folder. In other words, you don't need to use the "home folder name" property at all.

But if you do ever need to combine file paths, it's cleaner (though a little more work) to use a method like:

Code: Select all

$path = $folderPath.filePathByAppendingComponent($fileName)

Re: Open a file via Link

Posted: 2010-08-31 09:44:25
by loulesko
Okay, okay so your code is way more sexy than my code. What, you do this for a living or something? I replaced my macro with what you did, it works much better, not having to enter a name speeds things up considerably.

Thanks for that Martin. Also thanks for the tip.

Lou

Re: Open a file via Link

Posted: 2010-10-01 12:50:13
by Lorrymeggs
A modification to the topic
I am writing a scientific book with 400 graphs and pictures. It is increasingly difficult to keep the names of the graphs in mind and search for them...

Instead of importing a graph with shift cmd i: is it possible to make a macro which actually imports the graph, but when clicking on it, it opens the file in the refering program, which generated the file? (In my case photoshop and freehand)

So the picture also contains the link to the file and would be the link. At the end of the modification it would be necessary to reload the graph to see the changes...

Re: Open a file via Link

Posted: 2010-10-02 09:16:56
by Kino
Lorrymeggs wrote:Instead of importing a graph with shift cmd i: is it possible to make a macro which actually imports the graph, but when clicking on it, it opens the file in the refering program, which generated the file? (In my case photoshop and freehand)
Try this macro. Clicking on an image does not work because a macro cannot modify this kind of UI behaviour and it supports only inline images (not floating images), but . . .

• When you run it without selecting an image, [1] it invokes the Open dialog asking you to locate an image file to be inserted, [2] insert the image, and [3] set a link to the original file.

• When you run it with an image having a valid link selected, the original image file will be opened in the default application for the file type (the same application which opens it when you double click on it in the Finder).

• When you run it with an image not having any link or having a wrong link selected, it it invokes the Open dialog asking you to locate the new original image file.

Code: Select all

$sel = TextSelection.active
if $sel == undefined
	exit 'No active selection, exiting...'
end

$image = Image.selectedImage
$imageExists = true

if Defined $image
	$attr = $sel.text.displayAttributesAtIndex $sel.location
	if Defined $attr.link
		$path = $attr.link
		$path.findAndReplace '^file://', '', 'E-i'
		if File.canReadFromPath $path
			if File.isFolderAtPath $path
				Set Link ''  # remove link
				exit "'$path' is a folder!\nRun this macro again to set a new link."
			else
				Open URL $attr.link
				exit
			end
		else
			if File.existsAtPath $path
				exit "Cannot open '$path'.\nPermissions problem?"
			else
				Set Link ''  # remove link
				exit "'$path' does not exist.\nRun this macro again to set a new link."
			end
		end
	end
else
	$imageExists = false
end

$path = Choose File '', 'Select Original'
if $path == undefined
	exit  # canceled by user
else
	$image = Image.newFromFileAtPath $path
	if $image == undefined
		exit "'$path' does not seem to be an image file, exiting..."
	end
	$path = 'file://' & $path
	if $imageExists == false
		Image.insertInlineImage $image
	end
	Set Link $path
end
At the end of the modification it would be necessary to reload the graph to see the changes...
The macro below replaces the selected image having a valid link with the original image file you have modified. Note that it does not and cannot know if the original has been really modified. It tries to retain the current size settings but is not very accurate and I have noticed a glitch: the new versin of the selected image moves a bit.

Code: Select all

$sel = TextSelection.active
if $sel == undefined
	exit 'No active selection, exiting...'
end

$image = Image.selectedImage
if Defined $image
	$attr = $sel.text.displayAttributesAtIndex $sel.location
	if Defined $attr.link
		$path = $link = $attr.link
		$path.findAndReplace '^file://', '', 'E-i'
		if File.canReadFromPath $path
			$newImage = Image.newFromFileAtPath $path
			if Defined $newImage
				$w = $image.width / $image.sourceWidth
				$h = $image.height / $image.sourceHeight
				$w = Cast to Int $w * 100
				$h = Cast to Int $h * 100
				Image.insertInlineImage $newImage
				$objFind = '(?:\x20\x5Cobjscalex[0-9]+\x20\x5Cobjscaley[0-9]+)?'
				$objFind &= '(?=\x20{\x5C\*\x5Cobjdata)'
				$objReplace = '\x20\x5Cobjscalex' & $w
				$objReplace &= '\x20\x5Cobjscaley' & $h
				$picFind = '(?:\x20\x5Cpicscalex[0-9]+\x20\x5Cpicscaley[0-9]+)?'
				$picFind &= '(?=\x20\x5C(?:macpict|pngblip|jpegblip))'
				$picReplace = '\x20\x5Cpicscalex' & $w
				$picReplace &= '\x20\x5Cpicscaley' & $h
				$img = $sel.subtext
				$img = Encode RTF $img
				$img = Cast to String $img
				$img.findAndReplace $objFind, $objReplace, 'E-i'
				$img.findAndReplace $picFind, $picReplace, 'E-i'
				$img = Decode RTF $img
				$sel.text.replaceInRange $sel.range, $img
				TextSelection.setActiveRange $sel.range
				Set Link $link  # restore link
			else
				Set Link ''  # remove link
				exit "'$path' is not an image file."
			end
		else
			exit "Cannot read '$path', exiting..."
		end
	end
else
	exit 'No inline image selected, exiting...'
end
I have not tested those macro extensively and I don’t have Photoshop nor Freehand. They may not work as expected by you.

Edit: Corrected a bug in the first macro.

Re: Open a file via Link

Posted: 2010-10-04 05:52:51
by Lorrymeggs
Wow!! That's it! Very good! You don't know how much this means to me!

A small thing: the first macro works but produces an error in the last line after doing the job... but it still works! The error occurs, when linking to a previously unlinked graph.

an error in the macro "Open Link" has occured in line 48...
unknown path "end"

(Im Makro "Open link" ist in Zeile 48 ein Fehler aufgetreten.)

Re: Open a file via Link

Posted: 2010-10-04 06:10:21
by Kino
Lorrymeggs wrote:an error in the macro "Open Link" has occured in line 48...
unknown path "end"
Oh, sorry. Indeed, there is a bug.

Code: Select all

$path = Choose File '', 'Select Original'
if $path == undefined
		exit  # canceled by user
	end
should be

Code: Select all

$path = Choose File '', 'Select Original'
if $path == undefined
	exit  # canceled by user
else
I corrected the macro above posted in my previous message.
There may be other bugs. Sorry in advance ;-)

Re: Open a file via Link

Posted: 2010-10-04 07:15:27
by Lorrymeggs
You really safe me hours and hours of searching for the right graph! Absolutely happy with the solution!

Until now I had to work with Framemaker, which was very good but only runs on the aged MacOS 9. I was looking for a reasonable competitor, desperately tested many writing programs - at least 10... was very unhappy with what I saw and really at last started to work with Nisuswriter, which is by far the most suited writing program for a job like writing a scientific book! The click and open possibility in Framemaker was the only major drawback.
BUT:
After all this macro is it! It is time to say good bye to Framemaker!