Can anyone assist my almost working Windows .LNK shortcut AppleScript?

can anyone help me get finder to launch this script when a windoze shortcut ( .LNK file ) is double clicked, I would imagine its using something like this…

on run {input, parameters}
	repeat with i in input
		display dialog (i as text)
	end repeat
	
	return input
end run

I get… “The run handler was specified more than once, or there where top-level commands in addition to the run handler.” … if I include the above in the working script below :frowning: Help!

set f to (choose file with prompt "Select a file to read:")
set x to read (f as alias) as text
set y to ":\\"

repeat while not (x begins with y)
	set x to characters 2 thru -1 of x as string
end repeat
set x to characters 2 thru -1 of x as string
repeat while not (x begins with y)
	set x to characters 2 thru -1 of x as string
end repeat
set x to characters 2 thru -1 of x as string
set y to ASCII character of 0

repeat with i from 1 to (length of x)
	set z to (text i thru i of x)
	if z = y then
		set x to characters 1 thru (i - 1) of x as string
		exit repeat
	end if
end repeat
set x to StrTran(x, "\\", ":")

tell application "Finder"
	set DiskList to every disk
	repeat with ThisDisk in DiskList
		set Nod to name of ThisDisk
		set Nod2 to Nod & x
		if (the file Nod2 exists) then
			display dialog Nod2 & " - EXISTS!"
			tell application "Finder"
				open Nod2
			end tell
			exit repeat
		end if
	end repeat
end tell

on StrTran(the_string, search_string, replace_string)
	tell (a reference to my text item delimiters)
		set {old_tid, contents} to {contents, search_string}
		set {the_string, contents} to {the_string's text items, replace_string}
		set {the_string, contents} to {the_string as Unicode text, old_tid}
	end tell
	return the_string
end StrTran

Model: Mini Mac
AppleScript: 1.10.6
Browser: Firefox 1.5.0.3
Operating System: Mac OS X (10.4)

sorry the correct if file exists line should be…

if (the file Nod2 exists) or (the folder Nod2 exists) then

…still cant get applescript runtime parameters :frowning:

Help! Help!

Hi there :slight_smile:

I’ve seen your posts, but I have no idea about the .lnk specs. Nor I don’t know why you’re interested in preserving such .lnk files… Why don’t you simply convert them to aliases (Mac equivalent to Win’s shortcuts)? This would eliminate the overhead of launching an app for such a simple task…

About your script, seems a mix of something with something Automator-related?

Every time you write a simple script, for example “beep 5”, it is enclosed in an implicit “run” handler. So, this is equivalent:

beep 5

To:

on run
	beep 5
end run

And you can’t have two “run” handlers in the same script (the implicit and the explicit). That explains the compilation error.

Now, what is your question? What are you trying to do? Automator? Or a simple handler for double-clicked .lnk files?

Hi,
thanks for the reply, Im an ex-windoz user just switched to mac (& I love it) but I have to use :frowning: Windoze XP (work) - I carry a 200gb usb drive with me - from work (XP) to home (OSX) etc… as I work with vb writing accounting software, I was able to write a VB script on my work PC to correctly deal with any OSX aliases I create.
However my coding on OSX is not up to that level yet (as you noted :slight_smile: ) - I want OSX to be able to use XP created .LNKs

the code works - but it opens a dialog to “choose file” - I would like my script to be called whenever I double click any .LNK file and KNOW what .LNK file was double clicked

I’m sure this is possible - and from my limited Google-ing - the answer seems to lie with “on run {input, parameters}”

any help would be greatly appreciated :slight_smile:
and again thanks for the reply

I right clicked a .LNK file went to its information window
and changed “opens with” to a sample compiled script (it just dialog-ed “HI”)

was greeted with something like (sorry am typing this from work) “unknown if application can open document”

can AppleScripts be called when any .LNK file is double clicked?

if not I will have to go another route (hope not - I’m getting to like AS - and it reads very well)

OK. This is the how-to.

Setup an “open” handler. This will be invoked when the system requests your app to open a file (via drag & drop, double-click, etc.)

on open listOfFiles
	--> listOfFiles will be something as:
	--> {alias "path:to:file1.lnk", alias "path:to:file2.lnk"}
	--> loop and process each one
	repeat with i from 1 to count listOfFiles
		process(listOfFiles's item i)
	end repeat
end open

to process(thisFile) --> use your code here
	set x to read thisFile
	set y to ":\\"
	--> ...
end process

Step 2. Configure your app, so it lets know the system it can open .lnk files.

Save this code as Application Bundle. Check the “Stay Open” checkbox if you wish your app to be allways opened (useful if you will be double-clicking .lnk files all the time).
Now, go to the Finder, locate your app. Ctrl+click it, choose “Show Package Contents”, locate two files: “PkgInfo” and “Info.plist”.
Open the first one using TextEdit. You will see 8 bytes: APPLdplt. “APPL” is the “file type”. “dplt”, the creator type. This one is shared by all “droplets” (applescript apps with an “open” handler). So, we will change it, so the OS doesn’t get confused when we tell it that this app should handle “.lnk” files. I will use “LINK” (for example). So, the file contains now “APPLLINK”. Save. Close. Open “Info.plist”.
Locate the “CFBundleSignature” key. Change again “dplt” with “LINK”.
Now, we will modify the “CFBundleDocumentTypes”, which defines what kind of documents is this app able to open. Currently, it is configured to open “whatever”: files, folders, etc. We could finish here, and just use the “opens with” you found in the “info window” of .lnk files. But we’ll do it the smart way. Substitute the “CFBundleDocumentTypes” entry with this one:

Save. Close.

Finally, we will let know “LaunchServices” there is an app in the OS able to handle .lnk files: ctrl+click the app. Zip it (create compressed archive). Delete the original app. Empty trash. Unzip the zip. LaunchServices will read now the “Info.plist” file, and learn your app can open .lnk files. Now, if all is OK, when you double-click a .lnk file, the OS should ask something as “This is the first time you’re trying to open this file using the app blah… Continue?”.

Thankyou, Thankyou, Thankyou, JJ you are a god!

If you ever visit Dublin Ireland from Madrid
post a message & I will happily feed you <= your own bodyweight in Guinness :slight_smile:

…am counting the minutes till I can sit in traffic for 1/2 hour, then get home and give the above a try

many thanks

ps: I would love a good book on applescript - should I just go with
the normal O’Reilly type book… http://www.oreilly.com/catalog/applescpttdg/
or can you suggest a better one (or should I just keep using this site?)

worked! I can now finder my external drive on OSX or XP :slight_smile:

here it is…

on open listOfFiles
	--> listOfFiles will be something as:
	--> {alias "path:to:file1.lnk", alias "path:to:file2.lnk"}
	--> loop and process each one
	repeat with i from 1 to count listOfFiles
		process(listOfFiles's item i)
	end repeat
end open

to process(thisFile)
	set x to read thisFile
	--> set f to (choose file with prompt "Select a file to read:")
	--> set x to read (f as alias) as text
	set y to ":\\"
	
	repeat while not (x begins with y)
		set x to characters 2 thru -1 of x as string
	end repeat
	set x to characters 2 thru -1 of x as string
	repeat while not (x begins with y)
		set x to characters 2 thru -1 of x as string
	end repeat
	set x to characters 2 thru -1 of x as string
	set y to ASCII character of 0
	
	repeat with i from 1 to (length of x)
		set z to (text i thru i of x)
		if z = y then
			set x to characters 1 thru (i - 1) of x as string
			exit repeat
		end if
	end repeat
	set x to StrTran(x, "\\", ":")
	
	tell application "Finder"
		set DiskList to every disk
		repeat with ThisDisk in DiskList
			set Nod to name of ThisDisk
			set Nod2 to Nod & x
			if (the file Nod2 exists) or (the folder Nod2 exists) then
				--> display dialog Nod2 & " - EXISTS!"
				tell application "Finder"
					open Nod2
				end tell
				exit repeat
			else
				--> display dialog Nod2
			end if
		end repeat
	end tell
end process

on StrTran(the_string, search_string, replace_string)
	tell (a reference to my text item delimiters)
		set {old_tid, contents} to {contents, search_string}
		set {the_string, contents} to {the_string's text items, replace_string}
		set {the_string, contents} to {the_string as Unicode text, old_tid}
	end tell
	return the_string
end StrTran

I’m glad it worked…

Meanwhile, I’ll have some Guinness’s for you! :wink:

“The Definitive Guide” is a great book. I would, though (personally), spend my bucks in AppleScript-related software (when I find the opportunity. Ie: PreFab’s UI Browser, FastScripts, etc.).

You can find here a nice introduction (free & pdf):
http://applescriptsourcebook.com/articles/408_0_22_0_C/

Appart from that, we have here various interesting articles:
http://macscripter.net/unscripted
Loads of ready-to-use snippets:
http://bbs.applescript.net/viewforum.php?id=11
A FAQ:
http://applescriptsourcebook.com/
And more loads of open-source scripts:
http://scriptbuilders.net/

Once upon a time, I received in my birthday this book. It’s a nice add-on for my shelves, but not quite what I needed. I learned AppleScript scripting Quark and the Finder. So, I learned Quark and Finder scripting. Asking in this forum and similar sites. Reading others’ scripts, etc. And, usually, if you learn to drive one or two apps using AppleScript, you’ve learned lots of AppleScript. About the language itself, it’s very easy. If you programmed before (and you did), you will find it very easy to read and understand “handlers” (“functions” in other languages) and the basic data types (lists, numbers, etc.).

The difficult part is writing very clean, fast, efficient code. You must learn the tricks and workaround the various bugs and lacks-of-features in AppleScript :stuck_out_tongue:

But, if you like it, you’ll love it.

Thanks again jj,

I showed some of the other programmers here at work this site.
Very impressed with ease of language, and your sites great help!

I think we may have some more OSX converts from windoze :slight_smile: