How to reveal path to a file/folder

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

Post Reply
trevix
Posts: 387
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

How to reveal path to a file/folder

Post by trevix » Wed Feb 28, 2018 9:43 pm

I wonder if there is a way, given a path, to show the location of a file/folder on OSX and Windows.
Similar to what the system does with aliases, opening a window with the selected file/folder.

I could do it with Applescript, but I would like a solution for both platforms (may be using a shell command?).
Thanks for any help.

Trevix
Last edited by trevix on Fri Mar 02, 2018 4:16 pm, edited 1 time in total.
Trevix

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5485
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to display path to a file/folder

Post by dunbarx » Wed Feb 28, 2018 11:23 pm

Hi.

But if you already have a path ("...given a path..."), either because you just do or you obtain it with the "answer file" or "ask file" commands, don't you already know its "location", as you put it?

Craig Newman

trevix
Posts: 387
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: How to display path to a file/folder

Post by trevix » Wed Feb 28, 2018 11:27 pm

I was not clear enough:
I have a list of file paths and I would like, upon clicking on a path, to have the Finder (on OSX) or Windows to open the window where the file is and select the file
Trevix

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4566
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How to display path to a file/folder

Post by jacque » Thu Mar 01, 2018 7:59 pm

This little trick works on Mac and (probably) Windows:

Code: Select all

put <path/to/folder/" into tFolder
launch url ("file:" & tFolder)
if the result <> "" then answer the result -- error check here
Even though you've got a folder path, the "file" designation will open the folder. I don't know of a way to actually select the file, but the user will be in the right place.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

SparkOut
Posts: 1763
Joined: Sun Sep 23, 2007 4:58 pm

Re: How to display path to a file/folder

Post by SparkOut » Fri Mar 02, 2018 8:43 am

This thread may have some cross platform possible investigations to explore
http://forums.livecode.com/viewtopic.php?f=9&t=20058

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1737
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: How to display path to a file/folder

Post by [-hh] » Fri Mar 02, 2018 10:45 am

Nothing new but collects all hints from above in a cross-platform working script.
I have it in nearly all my newer stacks, mostly in a 8x8 square button at topleft.
Works with LC 6/7/8/9 on Mac/Win/Linux(all flavours I use)/Raspi.
It opens a selected folder of the path containing the file, alike clicking at top midth of an open window in most text editors.

Code: Select all

on mouseDown
  put the long id of this stack into M #<-- this is an example of a path
  set itemdelimiter to quote
  put item -1 of M into M
  set itemdelimiter to comma
  replace "/" with CR in M
  if line 1 of M is empty then put "[Top]" into line 1 of M
  if line 2 of M is empty then
    put "[]"& line 3 of M into line 1 to 3 of M
  end if
  put "(" before last line of M
  put M into me
end mouseDown

on mouseUp
end mouseUp

on mouseRelease
end mouseRelease

on menuPick
  goTo line 1 to (the menuhistory of me) of me -- in script of grp "stackPath"
end menuPick

on mouseEnter
  set tooltip of me to "Select from the path to this stack."
end mouseENter

on goTo t
   set itemdelimiter to "]"
   put item 1 of line 1 of t into t1
   if colon is in t1 then  -- do nothing
   else if "[Top" is t1 then put empty into line 1 of t
   else if "[" is t1 then put "//" into char 1 to 2 of t
   else put CR before t
   set itemdelimiter to comma
   if t is empty then put CR into t
   replace CR with "/" in t
   if the platform is "linux" then
      try
         launch document t
         get shell("xdg-open " & quote&t&quote)
      end try
   else launch document t
end goTo
Last edited by [-hh] on Fri Mar 02, 2018 4:33 pm, edited 1 time in total.
shiftLock happens

trevix
Posts: 387
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: How to display path to a file/folder

Post by trevix » Fri Mar 02, 2018 11:45 am

I give up. I think I am going to look for Applescript/VBscript solutions.
This works pretty good for OSX (replacing <ThePath> with a regular path to folder or files:

Code: Select all

tell application "Finder"
	activate
	try
	reveal (POSIX file "<ThePath>")
	on error
		return "error"
	end try
end tell
It confuses itself a little bit if you need to locate an app (Something.app), but I can go with it.
Googling for VBscript or Shell I found this:
https://stackoverflow.com/questions/136 ... e-selected
Not sure how to use it.
Trevix

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1737
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: How to display path to a file/folder

Post by [-hh] » Fri Mar 02, 2018 4:12 pm

[It's hard to understand what you want with that title. Please change "display" to "reveal".]

The following script reveals a file in the Finder/Desktop manager, that is it opens the containing folder and selects the file icon (or the file name in a list).
Works on Mac/Win/linux/Raspi.

Code: Select all

-- Some file path examples (on windows LC uses slash).
local f="/Users/admin/Documents/Spenden\ für\ Freies\ Wissen\ -\ Wikimedia\ Fördergesellschaft.pdf" -- Mac
local g="C:/Users/IEUser/Documents/minimal/stars1.png" -- Win
local h="/home/hh/Documents/blendDemoLinux.livecode" -- linux

on mouseUp
  switch the platform
    case "MacOS"
      get shell("open -R " & f)
      break
    case "Win32"
      set the hideConsoleWindows to true
      replace "/" with "\" in g
      get shell("explorer.exe /select, " & g)
      break
    case "linux"
      -- nautilus or nemo may not yet be installed
      -- get shell("nemo " & h) -- some use "nemo" instead of nautilus
      get shell("nautilus " & h)
      break
  end switch
end mouseUp
shiftLock happens

trevix
Posts: 387
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: How to reveal path to a file/folder

Post by trevix » Fri Mar 02, 2018 4:34 pm

Thanks for the help.

I tried on OSX Sierra and LC 8.1.9 but it does not works.
Given a path:
/Users/trevix/Documents/AAA_LavoriInCorso/Piccolo Installer/Test_Files/License.txt
the it var is set to:
The files /Users/trevix/Documents/AAA_LavoriInCorso/Piccolo and /Users/trevix/Library/Preferences/Piccolo_Installer_Preferences/Installer/Test_Files/License.txt do not exist.
I copied the path from the info of the files and I am sure is correct.
I think it has do do with the "space" on the path, but I cannot avoid of having it.
Regards
Trevix
Trevix

bogs
Posts: 2080
Joined: Sat Feb 25, 2017 10:45 pm

Re: How to reveal path to a file/folder

Post by bogs » Fri Mar 02, 2018 4:39 pm

Maybe enclose the path in single quotes, i.e. (psuedo)

set the path to "'" & the path & "'"
Image

trevix
Posts: 387
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: How to reveal path to a file/folder

Post by trevix » Fri Mar 02, 2018 5:02 pm

Perfect !!
Thanks
Trevix

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1737
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: How to reveal path to a file/folder

Post by [-hh] » Fri Mar 02, 2018 5:40 pm

Either use single quotes around the original file name, as bogs said, or use NO quotes and escape space like in my path example for Mac above.

Code: Select all

function sq t
   return "'"& t &"'"
end sq

function sp t
   replace space with "\ " in t
   return t
end spc
Sadly revealing several files in the same folder doesn't work as above. If the folder is already open then simply the selection changes.
Opening several windows for files in different folders works.
(Opening a new window for each call targeting the same folder could also conflict with general user settings).
shiftLock happens

bogs
Posts: 2080
Joined: Sat Feb 25, 2017 10:45 pm

Re: How to reveal path to a file/folder

Post by bogs » Fri Mar 02, 2018 5:57 pm

Happy to help trevix :D
Image

trevix
Posts: 387
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: How to reveal path to a file/folder

Post by trevix » Fri Mar 02, 2018 7:15 pm

Thanks very much for your help -hh.
I want to notice also that, on OSX, what happens running the script is slightly different if the path file is a document (yyy.txt) or an app (xxx.app).
It is as if the system does not know if to consider it a file or a folder (being a package).
Not a problem, tough.
Trevix

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1737
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: How to reveal path to a file/folder

Post by [-hh] » Fri Mar 02, 2018 7:37 pm

My script above selects not only files but also folders in their containing folder.

On MacOS an application (package) is actually a folder.
You could just select what you need in your scenario.

This opens the Application folder and selects (the "folder") TextEdit.app:

Code: Select all

get shell ("open -R /Applications/TextEdit.app")
And this opens the contents folder and selects the binary file:

Code: Select all

get shell ("open -R /Applications/TextEdit.app/Contents/MacOS/TextEdit") 
get shell ("open /Applications/TextEdit.app")
   launches TextEdit,
get shell ("open -n /Applications/TextEdit.app")
   launches a new instance of TextEdit,
get shell ("open /Applications/TextEdit.app/Contents/MacOS/TextEdit")
   launches Terminal (because it is a unix binary) which in turn launches TextEdit.
shiftLock happens

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”