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
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
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.
This handler may do what you want. The comments explain how it works.
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}
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
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
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
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):
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
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.
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:
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.
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).
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?
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 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.
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?!
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.
(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.
(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.
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.
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:
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
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.
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:
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.
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):
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.
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.
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:
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