Text editing

Hello,

I have now solved my convert win filepath to mac filepath.
And now i want to remove the second folder from the path, and then open the new path in the finder.

Now lets say that i have a path that looks something like this /N/folder1/folder2/folder3/file.ext

I want remove the folder2 from the text line, and i need to isolate the file.ext,
because i want to reveal the file in the finder, which as far as i can tell needs this syntax:

tell application “Finder” to reveal the original item of file “file.ext” in Mymacfilepath

So would it be smart to make a list/array etc. where each individual foldername is store, so i can make my path of the text of folder1/folder3/file.ext?

Anyone has any tips or tricks?

Best…jklarsen

Hi again,

Ok i figured out to remove the folder2 with a search/replace script…

Now my question is this, is there a reveal/show in finder function that works based on a unix path like this /N/folder1/folder3/file.ext ?

Best…jklarsen

A bit to fast on the trigger there.

What i discovered i need to is to swap folder2 and folder3 around in the path.

So…
/N/folder1/folder2/folder3 should look like this
/N/folder1/folder3/folder2

Still any help is more than welcome…

Best…jklarsen

In the Finder ⇧⌘G

Hello,

Doesnt the “go to folder” only work if it is only a folder, and not if the path also contains a file.ext.

Isnt there a way in applescript?

Best…jklarsen

you can easily coerce a POSIX path to a HFS path and reveal the file or folder in the Finder


set a to "/Applications/"
tell application "Finder" to reveal POSIX file a as alias

That did the Reveal/Show in finder trick!

Thank you very much.

Now i only have to figure out how to swith folders in a path.
So any of you out there has a easy solution, that would be much appreciated.

Best…jklarsen

I have everything apart from the switch folder in the path thing.
When i run it in the script editor it works fine.
But when i run it from through my right-click contextual menu (using) OnMyCommand,
in textedit, everything starts going wrong, textedit starts freezing, soon after finder goes down,
and soon everything is freezing and a reboot is the only fix.

this is the script.


tell application "TextEdit"
	activate
end tell

tell application "System Events"
	tell process "textedit"
		keystroke "c" using {command down}
	end tell
end tell

tell application "Finder"
	activate
	set thePath to the clipboard
end tell

on win2MacPath(WinPath)
	if WinPath starts with "j" then
		set DriveMount to "junk"
	else if WinPath starts with "n" then
		set DriveMount to "/Volumes/Septimus/"
	end if
	
	set my text item delimiters to "\\"
	set filePath to text items 2 through -1 of WinPath
	set MacPath to {DriveMount} & filePath
	set my text item delimiters to "/"
	return "/" & MacPath as text
end win2MacPath

tell application "Finder"
	activate
end tell

tell application "System Events"
	tell process "Finder"
		set a to my win2MacPath(thePath)
		tell application "Finder" to reveal POSIX file a as alias
	end tell
end tell

Have you experienced anything similar?

Best…jklarsen

I have never used OnMyCommand, so I can not offer much help there. When I did a search for “OnMyCommand” I found this at the bottom of the page, under the heading “Known problems”:

So, maybe it is a known problem with OnMyCommand.

You might want to look into something like ThisService. I have never had occasion to use it, but it is supposed to be able to create a Services menu item from AppleScript programs (or programs in other languages). You could then use that service from many different text-based applications (not just TextEdit).

As far as swaping items though, try this handler (I added one line to do the swap):

on win2MacPath(WinPath)
	if WinPath starts with "j" then
		set DriveMount to "junk"
	else if WinPath starts with "n" then
		set DriveMount to "/Volumes/Septimus/"
	end if
	
	set my text item delimiters to "\\"
	set filePath to text items 2 through -1 of WinPath
	tell filePath to set {item 1, item 2} to {item 2, item 1} -- swap item 1 and item 2; shorthand for this: set {item 1 of filePath, item 2 of filePath} to {item 2 of filePath, item 1 of filePath}
	set MacPath to {DriveMount} & filePath
	set my text item delimiters to "/"
	return "/" & MacPath as text
end win2MacPath

Hi,

And thank so very much for your help.

I guess it is really not necessary to send a “copy” event to the application, but i dont now how to get the selection otherwise,
since i tried with “set a to the selection” and that doesnt work anywhere else than in finder, so do you have any “get selection” tips
that doesnt include sending keystrokes?

Best…jklarsen

Not every application will support the idea of “the selection”. Based on its dictionary, it looks like TextEdit is one of the applications that does not support accessing the current selection.

However, the current selection is exactly what services (that list of stuff under “Services” in every application’s first menu) can access. The application that I mentioned (ThisService) lets you write additions to the Services submenu. You can use services from many applications (TextEdit, Terminal, Mail, etc.).

Since you are dealing with the selection and your goal (reveal a file in Finder) is really unrelated to the source of the selection (the Window-style path text), a service seems like a perfect solution.

But like I said, I have never used ThisService, so I can not offer any good help for using it. I do use various services regularly, but I have never needed to use ThisService to create my own custom service.

Frankly, the limitation of OnMyCommand that it can not reliably send AppleScript events to the application from which it was triggered seems like a major stumbling block to me (not for all purposes, but certainly for a great many). Based on the description of the “known problem” on the OnMyCommand page, I expect that your initial activate and probably also the Command-C keystroke are bound to cause the lock-up problems you have reported. From the documentation of OnMyCommand that I have been able to find, it seems that you should be able to skip the idea of explicitly asking for the selection or faking a Command-C to copy it. It looks like you are supposed to just use a special token called OBJ_TEXT in your (AppleScript) program. You could try that, but unless your solution absolutely has to be done in a context menu, I would look for a different solution (OnMyCommand seems clever, but awfully rough around the edges for a casual user).