Tuesday, May 11, 2021

#1 2021-03-31 02:30:37 pm

vanwoods
Member
Registered: 2021-03-28
Posts: 14

Combine file/folder path selection to windows link

Hi all,

Thank you for being here in the first place! Just new to scripting, so hope you can help me out here

We are working in a mac environment at the office. Since a month there is windows computer joining us. We are using links to files and folders by email, which work fine under the osx users. I already found a usefull script here to convert it to windows link. But i need to combine two scripts in one so it converts the selection and i can access it to a keystroke. Below are the two scripts, can anybody tell me if it's possible?

OSX selection script

Applescript:


return expand("")

on expand(separator)
   
   tell application "Finder"
       set theSelection to get selection as list
       set theResult to ""
       if length of separator = 0 then set separator to linefeed
       repeat with theItem in theSelection
           if length of theResult > 0 then set theResult to theResult & separator
           set theResult to theResult & POSIX path of (theItem as alias)
       end repeat
       return theResult
   end tell
   
end expand

Copy Windows File Path (found here on the forum https://macscripter.net/viewtopic.php?pid=107995)

Applescript:


on run
tell application "Finder" to set sel to selection
if sel is "" then display dialog "Nothing selected" buttons {"Cancel"} default button 1
open sel
end run

on open theseItems
set resultList to {}
set TID to text item delimiters
repeat with oneFile in theseItems
set text item delimiters to ":"
set filePath to text items of (oneFile as text)
set text item delimiters to "\\"
set end of resultList to "\\\\" & filePath as text
end repeat
set text item delimiters to return
set resultList to resultList as text
set text item delimiters to TID
set the clipboard to resultList
end open

Already tried some combinations by combining both scripts but getting a <?> as result? Hope to find some help here in combining these scripts.

Thanks in advance!

Greetz,
vanWoods

Last edited by vanwoods (2021-03-31 02:33:07 pm)

Offline

 

#2 2021-04-01 11:40:51 am

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 377

Re: Combine file/folder path selection to windows link

This handler may do what you want.  The comments explain how it works.

Applescript:


on pathsFromSelection()
   -- Returns the HFS, Windows, and POSIX paths of items currently selected in the Finder
   -- The code is wrapped in a try block so that the text item delimiters are restored to their baseline value even if an error occurs
   set TID to AppleScript's text item delimiters
   try
       -- The <selection> command returns the currently selected Finder items as a list of Finder file references
       -- <as text> transforms those Finder file references into a single text string of concatenated HFS paths, separated from one another by the current <text item delimiters> string
       -- Thus, with text item delimiters set to the linefeed character, the <selection as text> expression returns a multiline text string with HFS paths on separate lines
       -- The <paragraphs> property returns the multiline text string's content as a list of HFS paths
       set AppleScript's text item delimiters to linefeed
       tell application "Finder" to set HFSPaths to (selection as text)'s paragraphs
       set {windowsPaths, POSIXPaths} to {{}, {}}
       repeat with currHFSPath in HFSPaths
           -- For each HFS path, get its Windows path by first extracting the HFS path's colon-delimited components ( <set AppleScript's text item delimiters to ":"> and <tell (get currHFSPath's text items))> ), then transforming the components into a single reverse-slash delimited string prefixed with two reverse-slashes ( <set AppleScript's text item delimiters to "\\"> and <set end of windowsPaths to "\\\\" & it> )
           set AppleScript's text item delimiters to ":"
           tell (get currHFSPath's text items)
               set AppleScript's text item delimiters to "\\"
               set end of windowsPaths to "\\\\" & it
           end tell
           -- For each HFS path, get its POSIX path directly from its <POSIX path> property
           set end of POSIXPaths to currHFSPath's POSIX path
       end repeat
       -- The results will be returned as lists of paths; if the results should be returned as multiline text strings, un-comment the following two lines of code
       --set AppleScript's text item delimiters to linefeed
       --set {HFSPaths, windowsPaths, POSIXPaths} to {HFSPaths as text, windowsPaths as text, POSIXPaths as text}
   end try
   set AppleScript's text item delimiters to TID
   -- The results are returned as an AppleScript record
   return {HFSPaths:HFSPaths, windowsPaths:windowsPaths, POSIXPaths:POSIXPaths}
end pathsFromSelection

set {myHFSPaths, myWindowsPaths, myPOSIXPaths} to my pathsFromSelection()'s {HFSPaths, windowsPaths, POSIXPaths}

Last edited by bmose (2021-04-01 12:22:35 pm)

Offline

 

#3 2021-04-01 12:44:29 pm

vanwoods
Member
Registered: 2021-03-28
Posts: 14

Re: Combine file/folder path selection to windows link

Hi bmose,

Thanx for your quick reply! Unfortunately i can't get this to work properly. I'm getting an <?> as result when i'm trying to paste the result in for example an email.

I already have the script (2) below working as an app. But the thing is that i need to combine it to a keystroke with Typinator. So my guess was that is has something to do with the expand function from the first script (1) i had posted. As i have that script already working from within Typinator, but this one is only for POSIX Paths.

So basically what i'm looking for is script 1 that returns the WindowsPath (script 2) instead of the POSIX Paths.

Do you think this is possible?

Script 1

Applescript:


return expand("")

on expand(separator)

tell application "Finder"
set theSelection to get selection as list
set theResult to ""
if length of separator = 0 then set separator to linefeed
repeat with theItem in theSelection
if length of theResult > 0 then set theResult to theResult & separator
set theResult to theResult & POSIX path of (theItem as alias)
end repeat
return theResult
end tell

end expand

Script 2

Applescript:


on run
tell application "Finder" to set sel to selection
if sel is "" then display dialog "Nothing selected" buttons {"Cancel"} default button 1
open sel
end run

on open theseItems
set resultList to {}
set TID to text item delimiters
repeat with oneFile in theseItems
set text item delimiters to ":"
set filePath to text items of (oneFile as text)
set text item delimiters to "\\"
set end of resultList to "\\\\" & filePath as text
end repeat
set text item delimiters to return
set resultList to resultList as text
set text item delimiters to TID
set the clipboard to resultList
end open

Offline

 

#4 2021-04-01 01:19:57 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 377

Re: Combine file/folder path selection to windows link

It seems that you want to launch your app and have the Windows paths of items currently selected in the Finder saved to the clipboard. If that is the case, then try replacing your app's code (your Script 2) with the following code (there should be no need for your Script 1):

Applescript:


set TID to AppleScript's text item delimiters
try
   set AppleScript's text item delimiters to linefeed & "\\\\"
   tell application "Finder" to set tmpPaths to "\\\\" & selection
   if tmpPaths is "\\\\" then display dialog "Nothing selected" buttons {"Cancel"} default button 1
   set AppleScript's text item delimiters to ":"
   tell (get tmpPaths's text items)
       set AppleScript's text item delimiters to "\\"
       set windowsPaths to it as text
   end tell
   set the clipboard to windowsPaths
end try
set AppleScript's text item delimiters to TID

Offline

 

#5 2021-04-01 02:16:34 pm

vanwoods
Member
Registered: 2021-03-28
Posts: 14

Re: Combine file/folder path selection to windows link

Yes that is right. Your script is working and returns the correct windowspath when the script is used and the result can be pasted anywhere. Unfortunately the program (typinator) which work with abbreviations (keystroke combination) only returns one backslash (\). I have tried to add the expand function to the script but this gives me the same result.

Still wondering what makes script 1 returns the complete path (POSIX) after typing the abbreviations.

I can still use this script as an app so maybe that wil be the option for now.

Offline

 

#6 2021-04-01 02:39:30 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 377

Re: Combine file/folder path selection to windows link

By "one backslash", I'm guessing that you are getting this for your Windows paths:

\\...\...\...\...

but would rather get this:

\\...\\...\\...\\...

If that is the case, then simply modifying the text item delimiters statement on line 8 of the code should give you what you want:

Applescript:


set TID to AppleScript's text item delimiters
try
   set AppleScript's text item delimiters to linefeed & "\\\\"
   tell application "Finder" to set tmpPaths to "\\\\" & selection
   if tmpPaths is "\\\\" then display dialog "Nothing selected" buttons {"Cancel"} default button 1
   set AppleScript's text item delimiters to ":"
   tell (get tmpPaths's text items)
       set AppleScript's text item delimiters to "\\\\"
       set windowsPaths to it as text
   end tell
   set the clipboard to windowsPaths
end try
set AppleScript's text item delimiters to TID

If that is not what you are looking for, you'll have to explain what you need more clearly.

Offline

 

#7 2021-04-01 03:06:17 pm

vanwoods
Member
Registered: 2021-03-28
Posts: 14

Re: Combine file/folder path selection to windows link

Sorry, i think i didn't explained it correctly. Your script works fine, but not from within Typinator.

Only when i use Typinator it only gives an "\" as the result after typing the abbreviatipn. Nothing more just one backslash. The program somehow doesn't handle this script the way it handles script 1.

Below a screenshot of typinator. It is the third abbreviation, so when typed 'ppw' it should execute the script (win2.applesript).
typinator

Offline

 

#8 2021-04-01 03:53:37 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 377

Re: Combine file/folder path selection to windows link

From what you describe, it appears that the problem is with the way Typinator is handling the results of the AppleScript script rather than with the script itself. I'm not sure I can help you with this.

Just to clarify a couple of points, are you using the script exactly as displayed on this website, or have you modified it in any way? Also, regarding the fact that Script 1 executes properly in Typinator, that script returns POSIX paths, which typically don't contain any backslash characters, whereas Windows paths do contain backslash characters (as file component separators.) Is it possible that Typinator is treating the backslash characters as special metacharacters rather than simply as literal characters?

Offline

 

#9 2021-04-02 05:13:34 am

vanwoods
Member
Registered: 2021-03-28
Posts: 14

Re: Combine file/folder path selection to windows link

Yes i used it exactly as u posted it here.

I guess it looks like a limitation of typinator as the script functions normal in osx. I'll try to contact them and find out if the problem is about the handling of the windows path.

I'll get back here if find the solution.

Thanks so far!

Offline

 

#10 2021-04-02 07:41:32 am

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 377

Re: Combine file/folder path selection to windows link

The plot thickens...

I have been a longtime fan of the creator of Typinator (specifically their app PopChar that provides quick and easy access to the entire Unicode character set.) Out of curiosity, I downloaded a trial version of Typinator and tried to reproduce your situation exactly with the following steps:

(1) Created a script in Script Editor (which I presume you are using as your script editor) with the code from my last post
(2) Saved the script as a text file named win2.applescript in the /Users/[home folder name]/Library/Application Support/Typinator/Sets/Includes/Scripts/ folder
(3) Created an abbreviation named ppw for win2.applescript in Typinator
(4) Selected several files in the Finder
(5) Opened a text document and typed the letters ppw
(6) Typinator replaced ppw with a tab character (which I then deleted)
(6) Pressed Command-v (the "paste" keystroke combination) to paste the current clipboard contents
(7) Sure enough, the Windows paths were properly pasted to my document.

So it works on my end. I would suggest going through each of the steps above and see if you are doing something different in one or more of the steps.

Last edited by bmose (2021-04-02 07:42:29 am)

Offline

 

#11 2021-04-02 08:11:10 am

vanwoods
Member
Registered: 2021-03-28
Posts: 14

Re: Combine file/folder path selection to windows link

Yes they make some really usefull apps, PopChar is also a fine app to use! Typinator is really handy because it let's you precompose standard emails with variables in it.

I completely used it as you just described it. So it is true that you can use the paste command after typing the abbreviation (which only leaves a backslash character on my side). Only when the script is used in for example a complete textual email it doesn't automatically enters the file path. When using the built in FinderSelection script (FinderSelection.applescript in includes folder) of typinator it automatically expands the complete path of the selection after typing the abbreviation.

So that is why I it thaught it had something to do with the expand function in Script 1?!

Offline

 

#12 2021-04-02 08:24:54 am

vanwoods
Member
Registered: 2021-03-28
Posts: 14

Re: Combine file/folder path selection to windows link

I've now changed it by adding the a keystroke after the script in typinator. See below:

{Scripts/win2.applescript}{key:⌘V}

It now only has three (\\\) backslashes at the beginning of the link instead of two (\\). Not find the solution to fix this yet.

Don't think that above should be the correct way to use (if you look at the way FinderSelection.applescript works) but if this is the only solution then it's fine by me.

Offline

 

#13 2021-04-02 08:33:02 am

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 377

Re: Combine file/folder path selection to windows link

Just a couple of further questions:

(1) Do you want one or two backslash characters separating components in the Windows file paths?  (Or do you want two at the start of the path, and one between subsequent path components?)

(2) Can you please describe step-by-step how you use Typinator in your textual emails and the behavior you would like to see? With a little bit of tweaking, we should be able to get your script to do exactly what you want.

Offline

 

#14 2021-04-02 09:08:35 am

vanwoods
Member
Registered: 2021-03-28
Posts: 14

Re: Combine file/folder path selection to windows link

(1)
Our windows links only need two backslashes at the beginning and the addition of the server name (which only needs to be added for windows). Only one backslash is now automatically placed extra at the beginning after typing the abbreviation (ppw), but that is not the problem of the script it is placed by typinator. We have two servers (SD and NT) which have 2 different internal ip-addresses, it it possible for the script to see on which server it copies the link and then add SD or NT before the link?

So it should be like this:
\\DS\path\to\selection\ or \\NT\path\to\selection\

Instead of this:
\\\path\to\selection\ (the first backslash is added by typinator for i don't know which reason)

The script needs to add 'SD or NT' (server name for windows), that depends on which server the link is copied from.

(2)
We are using a complete precomposed textual email which shows a link to the file or folder and some specifications which can be sent to the client.

So win2.applescript is part of the precomposed email.

Last edited by vanwoods (2021-04-02 09:09:01 am)

Offline

 

#15 2021-04-02 09:14:13 am

vanwoods
Member
Registered: 2021-03-28
Posts: 14

Re: Combine file/folder path selection to windows link

I can also create 2 scripts for both servers. But if it's possible to let the script see from which ip it is copying the link and let it add the correct addition (SD or NT) than it would be awesome.

Last edited by vanwoods (2021-04-02 09:14:40 am)

Offline

 

#16 2021-04-02 10:58:35 am

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 377

Re: Combine file/folder path selection to windows link

vanwoods wrote:

So it should be like this:
\\DS\path\to\selection\ or \\NT\path\to\selection\

I tweaked the script so that it should put two slashes only at the beginning of paths. Also, when you type ppw, the ppw should now be replaced by the paths without the need for a Command-v keystroke, so you should remove that keystroke from your Typinator entry:

Applescript:


set windowsPaths to ""
set TID to AppleScript's text item delimiters
try
   set AppleScript's text item delimiters to linefeed & "\\\\"
   tell application "Finder" to set tmpPaths to "\\\\" & selection
   if tmpPaths is "\\\\" then display dialog "Nothing selected" buttons {"Cancel"} default button 1
   set AppleScript's text item delimiters to ":"
   tell (get tmpPaths's text items)
       set AppleScript's text item delimiters to "\\"
       set windowsPaths to it as text
   end tell
end try
set AppleScript's text item delimiters to TID
return windowsPaths

vanwoods wrote:

We have two servers (SD and NT) which have 2 different internal ip-addresses, it it possible for the script to see on which server it copies the link and then add SD or NT before the link?

I don't have the experience with files on servers to answer that question, but it seems likely that adding a server prefix to the paths should be scriptable.

Last edited by bmose (2021-04-02 10:59:51 am)

Offline

 

#17 2021-04-02 11:57:51 am

vanwoods
Member
Registered: 2021-03-28
Posts: 14

Re: Combine file/folder path selection to windows link

Thanks bmose, it works great now!
For now i'll start with 2 scripts, with a fixed prefix for the link. So i tweaked the script a little:

Applescript:


set windowsPaths to ""
set TID to AppleScript's text item delimiters
try
   set AppleScript's text item delimiters to linefeed & "\\"
   tell application "Finder" to set tmpPaths to "\\" & selection
   if tmpPaths is "\\" then display dialog "Nothing selected" buttons {"Cancel"} default button 1
   set AppleScript's text item delimiters to ":"
   tell (get tmpPaths's text items)
       set AppleScript's text item delimiters to "\\"
       set windowsPaths to "\\\\SD" & it as text
   end tell
end try
set AppleScript's text item delimiters to TID
return windowsPaths

I'll make the expand of the abbreviation choose to use script A or B. So it is working great now!

Thanks a lot!


I'll try to figure out if there is an option to let the script see what server (ip-address) the link is coming from. I'll post it here if i find out how.

Offline

 

#18 2021-04-02 01:00:54 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 377

Re: Combine file/folder path selection to windows link

Glad you've got it working.

I found an old post in which DJ Bazzie Wazzie and McUsr discuss a way to get server information from file references. A key to their approach is bash's mount command, which returns descriptive information on all mounted servers. I'm happy to help you pursue this further if you'd like. In order to do so, I would need to see the value returned by the following command (with both Windows servers mounted):

Applescript:


do shell script "mount"

The only lines I would need to see are those referring to your Windows servers. Any remaining lines can be deleted. Of course, this is only if you want to pursue this further. Another option would be to email me via Messages.

Last edited by bmose (2021-04-02 01:02:14 pm)

Offline

 

#19 2021-04-02 01:46:32 pm

vanwoods
Member
Registered: 2021-03-28
Posts: 14

Re: Combine file/folder path selection to windows link

Oh wow, yes if you see a possible solution there. I also saw that script you are referring to but i didn't saw a possible solution in there yet.

Below is the requested info of the mounted servers. The servers have multiple server shares do you think you need them all or is it just the 2 different ip addresses? The servers are linux based (synology), not windows.

//user@192.168.100.25/SD%20Studio on /Volumes/SD Studio (afpfs, nodev, nosuid, mounted by user)
//user@192.168.100.20/NT%20Studio on /Volumes/NT Studio (afpfs, nodev, nosuid, mounted by user)

Let me know if you need something else?

Offline

 

#20 2021-04-02 03:14:47 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 377

Re: Combine file/folder path selection to windows link

It may not be necessary to use bash's mount command but instead simply to get each item's volume name from its Finder reference's disk property. Give this a try. It is a first attempt that may need tweaking:

Applescript:


set windowsPaths to ""
set TID to AppleScript's text item delimiters
try
   set windowsPathsList to {}
   tell application "Finder"
       repeat with currRef in (get selection)
           set {volumeName, HFSPath} to {currRef's disk's name, currRef as text}
           if {volumeName} is in {"SD Studio", "NT Studio"} then
               set windowsPathPrefix to "\\\\" & volumeName's text 1 thru 2
               set AppleScript's text item delimiters to ":"
               tell (get HFSPath's text items)
                   set AppleScript's text item delimiters to "\\"
                   set end of windowsPathsList to windowsPathPrefix & it
               end tell
           end if
       end repeat
   end tell
   set AppleScript's text item delimiters to linefeed
   set windowsPaths to windowsPathsList as text
end try
set AppleScript's text item delimiters to TID
return windowsPaths

Last edited by bmose (2021-04-02 03:15:42 pm)

Offline

 

#21 2021-04-02 03:51:17 pm

vanwoods
Member
Registered: 2021-03-28
Posts: 14

Re: Combine file/folder path selection to windows link

Good first shot! Only missing the backslash between SD and SD Studio.

\\SDSD Studio\2021\

The only thing is that it doesn't work with the other server shares that we have. Let me describe the situation.

Server SD 192.168.100.25 (add SD before links)
- SD Studio
- Share A
- Share B
- Share C

Server NT 192.168.100.20 (add NT before all links)
- NT Studio
- Share F
- Share R
- Share Y

'Share' is not the real name of the shares. Do you need the exact names of the server shares?

Last edited by vanwoods (2021-04-02 03:52:41 pm)

Offline

 

#22 2021-04-02 03:59:14 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 377

Re: Combine file/folder path selection to windows link

Yes, please provide the exact share names. And thank you for pointing out the missing backslash. That's easily put back in, which will be done after getting the other share names.

Last edited by bmose (2021-04-02 03:59:48 pm)

Offline

 

#23 2021-04-02 04:05:36 pm

vanwoods
Member
Registered: 2021-03-28
Posts: 14

Re: Combine file/folder path selection to windows link

Ok see below the share names.

Server SD (add SD before links)
- SD Studio
- SD Archive
- Cloche Studio B

Server NT (add NT before all links)
- NT Studio
- 0920 Studio
- 0920 Archive
- Cloche Studio A

Offline

 

#24 2021-04-02 04:15:14 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 377

Re: Combine file/folder path selection to windows link

Let's try this:

Applescript:


set windowsPaths to ""
set TID to AppleScript's text item delimiters
try
   set windowsPathsList to {}
   tell application "Finder"
       repeat with currRef in (get selection)
           set {volumeName, HFSPath, volumeID} to {currRef's disk's name, currRef as text, missing value}
           if {volumeName} is in {"SD Studio", "SD Archive", "Cloche Studio B"} then
               set volumeID to "SD"
           else if {volumeName} is in {"NT Studio", "0920 Studio", "0920 Archive", "Cloche Studio A"} then
               set volumeID to "NT"
           end if
           if volumeID ≠ missing value then
               set AppleScript's text item delimiters to ":"
               tell (get HFSPath's text items)
                   set AppleScript's text item delimiters to "\\"
                   set end of windowsPathsList to "\\\\" & volumeID & "\\" & it
               end tell
           end if
       end repeat
   end tell
   set AppleScript's text item delimiters to linefeed
   set windowsPaths to windowsPathsList as text
end try
set AppleScript's text item delimiters to TID
return windowsPaths

Last edited by bmose (2021-04-02 04:16:14 pm)

Offline

 

#25 2021-04-02 04:23:25 pm

vanwoods
Member
Registered: 2021-03-28
Posts: 14

Re: Combine file/folder path selection to windows link

Works like a charm! big_smile

Testing it over VPN. I'm gonna test this when i'm at the office monday, but i don't foresee any problems.

Thank you a lot bmose!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)