Open a file via Link
Open a file via Link
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
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
Put the insertion point somewhere in a hyper-link, e.g. “file:///Users/me/Documents/mylife.pdf”, and run this macro.
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’.
Code: Select all
$sel = TextSelection.active
$attr = $sel.text.displayAttributesAtIndex $sel.location
if $attr.link != undefined
Open URL $attr.link
end
Re: Open a file via Link
As always, thank yu very much Kino.
Lou
Lou
Re: Open a file via Link
Please post your macro, Lou.
Steve
Steve
Re: Open a file via Link
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
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
- martin
- Official Nisus Person
- Posts: 5230
- Joined: 2002-07-11 17:14:10
- Location: San Diego, CA
- Contact:
Re: Open a file via Link
Thanks for sharing your macro Lou. I was just about to post my own version of your macro:
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:
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 = Choose File '', 'Link To'
If $path
$link = "file://$path"
$name = $path.lastFilePathComponentWithoutExtension
Type Link $link, $name
End
One tip on your code:
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.$filepath = User Property “home folder path”
$foldername = User Property “home folder name”
$filepath = "$filepath/$foldername"
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
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
Thanks for that Martin. Also thanks for the tip.
Lou
-
- Posts: 3
- Joined: 2010-03-21 01:55:29
Re: Open a file via Link
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...
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
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 . . .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)
• 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
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.At the end of the modification it would be necessary to reload the graph to see the changes...
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
Edit: Corrected a bug in the first macro.
Last edited by Kino on 2010-10-04 06:09:21, edited 1 time in total.
-
- Posts: 3
- Joined: 2010-03-21 01:55:29
Re: Open a file via Link
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.)
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
Oh, sorry. Indeed, there is a bug.Lorrymeggs wrote:an error in the macro "Open Link" has occured in line 48...
unknown path "end"
Code: Select all
$path = Choose File '', 'Select Original'
if $path == undefined
exit # canceled by user
end
Code: Select all
$path = Choose File '', 'Select Original'
if $path == undefined
exit # canceled by user
else
There may be other bugs. Sorry in advance ;-)
-
- Posts: 3
- Joined: 2010-03-21 01:55:29
Re: Open a file via Link
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!
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!