Make a tiny url (or shorten url) of theResult

Hi,

I’m figuring out if there is a way to change theResult in the script below as a tiny or other shortened url?

	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)
		set theResult to ReplaceText(theResult, "/Volumes/DISK_A", "https://download.com") of me
		set theResult to ReplaceText(theResult, space, "%20") of me
	end repeat
	return theResult -- & linefeed
end tell

I have a tiny url script below but don’t know how to combine these?

on expand(theURL) -- parameter: URL
try
	if length of theURL = 0 then set theURL to (the clipboard as string)
	ignoring case
		if ((characters 1 through 4 of theURL as string) is not "http") then
			error "invalid URL " & theURL & " (must begin with \"http\")"
		else
			set curlCMD to ¬
				"curl --stderr /dev/null \"http://tinyurl.com/api-create.php?url=" & theURL & "\""
			set tinyURL to (do shell script curlCMD)
			return tinyURL
		end if
	end ignoring
on error (msg)
	return msg
end try

end expand

Or is there a better solution then the above tiny url script?

Thanks,
vanWoods

try following

createTinyURL(combineURLs(space))

on combineURLs(separator)
	tell application "Finder" to set theSelection to selection as alias list
	if length of separator = 0 then set separator to space
	set theResult to ""
	repeat with theItem in theSelection
		if length of theResult > 0 then set theResult to theResult & separator
		set theResult to theResult & POSIX path of theItem
		set theResult to replaceText(theResult, "/Volumes/DISK_A", "https://download.com") of me
		set theResult to replaceText(theResult, space, "%20") of me
	end repeat
	return theResult
end combineURLs

on replaceText(theText, fromTHIS, toTHIS)
	set ATID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to fromTHIS
	set theResult to text items of theText
	set AppleScript's text item delimiters to toTHIS
	set theResult to theResult as text
	set AppleScript's text item delimiters to ATID
	return theResult
end replaceText

on createTinyURL(theURL) -- parameter: URL
	if length of theURL = 0 then set theURL to (the clipboard as text)
	ignoring case
		if (text 1 thru 4 of theURL) is not "http" then return "invalid URL " & theURL & " (must begin with \"http\")"
		set tinyURL to (do shell script "curl --stderr /dev/null \"http://tinyurl.com/api-create.php?url=" & theURL & "\"")
	end ignoring
end createTinyURL

Thanks for your solution, it works fine as you have send it. I now only needed to add some extra network shares (DISK_A, DISK_B, DISK_C) with login credentials (appended to theResult) for every network share, Only now it places the login credentials off all the shares in the link? This makes the link unusable, any idea how I can fix this. See the modified script below:

createTinyURL(combineURLs(space))

on combineURLs(separator)
	tell application "Finder" to set theSelection to selection as alias list
	if length of separator = 0 then set separator to space
	set theResult to ""
	repeat with theItem in theSelection
		if length of theResult > 0 then set theResult to theResult & separator
		set theResult to theResult & POSIX path of theItem
		set theResult to replaceText(theResult & "?login=User_A:password_A", "/Volumes/DISK_A", "https://download.url.com") of me
		set theResult to replaceText(theResult & "?login=User_B:password_B", "/Volumes/DISK_B", "https://download.url.com") of me
		set theResult to replaceText(theResult & "?login=User_C:password_C", "/Volumes/DISK_C", "https://download.url.com") of me
		set theResult to replaceText(theResult, space, "%20") of me
	end repeat
	return theResult
end combineURLs

on replaceText(theText, fromTHIS, toTHIS)
	set ATID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to fromTHIS
	set theResult to text items of theText
	set AppleScript's text item delimiters to toTHIS
	set theResult to theResult as text
	set AppleScript's text item delimiters to ATID
	return theResult
end replaceText

on createTinyURL(theURL) -- parameter: URL
	if length of theURL = 0 then set theURL to (the clipboard as text)
	ignoring case
		if (text 1 thru 4 of theURL) is not "http" then return "invalid URL " & theURL & " (must begin with \"http\")"
		set tinyURL to (do shell script "curl --stderr /dev/null \"http://tinyurl.com/api-create.php?url=" & theURL & "\"")
	end ignoring
end createTinyURL

So it now makes:
https://download.url.com/?login=User_A:password_A?login=User_B:password_B?login=User_C:password_C

But I only need:
https://download.url.com/?login=User_A:password_A

I made some changes, but can’t test myself, because I have not accounts on tinyurl.com service. Try yourself:

createTinyURL(combineURLs(space), "?login=User_A:password_A")

on combineURLs(separator)
	tell application "Finder" to set theSelection to selection as alias list
	if length of separator = 0 then set separator to space
	set theResult to ""
	repeat with theItem in theSelection
		if length of theResult > 0 then set theResult to theResult & separator
		set theResult to theResult & POSIX path of theItem
		set theResult to replaceText(theResult, "/Volumes/DISK_A", "https://download.url.com") of me
		set theResult to replaceText(theResult, space, "%20") of me
	end repeat
	return theResult
end combineURLs

on replaceText(theText, fromTHIS, toTHIS)
	set ATID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to fromTHIS
	set theResult to text items of theText
	set AppleScript's text item delimiters to toTHIS
	set theResult to theResult as text
	set AppleScript's text item delimiters to ATID
	return theResult
end replaceText

on createTinyURL(theURL, loginPart)
	if length of theURL = 0 then set theURL to (the clipboard as text)
	ignoring case
		if (text 1 thru 4 of theURL) is not "http" then return "invalid URL " & theURL & " (must begin with \"http\")"
		set tinyURL to (do shell script "curl --stderr /dev/null \"http://tinyurl.com/api-create.php?url=" & theURL & loginPart & "\"")
	end ignoring
end createTinyURL

Sorry, I didn’t explained my last sentence good enough. The script that you just sent works, but only with login credentials for DISK_A in the output. Every share (DISK_A, DISK_B, DISK_C) has unique login credentials. Now it always uses ?login=User_A:password_A

  • DISK_A uses ?login=User_A:password_A
  • DISK_B uses ?login=User_B:password_B
  • DISK_C uses ?login=User_C:password_C

Is it possible with the login credentials in these lines:
set theResult to replaceText(theResult & "?login=User_A:password_A", "/Volumes/DISK_A", "https://download.url.com") of me
set theResult to replaceText(theResult & "?login=User_B:password_B", "/Volumes/DISK_B", "https://download.url.com") of me
set theResult to replaceText(theResult & "?login=User_C:password_C", "/Volumes/DISK_C", "https://download.url.com") of me

Hope I explained it clearly?

It’s hard for me, without having your service and accounts, to build everything that you need in my imagination. I hope I understood you.

Following script should create the tiny URL using appropriate login (depending on what volume you made selection)

tell application "Finder" to set theSelection to selection as alias list
set control_PosixPath to POSIX path of (item 1 of theSelection)

if control_PosixPath begins with "/Volumes/DISK_A" then
	set diskPath to "/Volumes/DISK_A"
	set loginPart to "?login=User_A:password_A"
	set downloadURL to "https://download.url_A.com"
else if control_PosixPath begins with "/Volumes/DISK_B" then
	set diskPath to "/Volumes/DISK_B"
	set loginPart to "?login=User_B:password_B"
	set downloadURL to "https://download.url_B.com"
else if control_PosixPath begins with "/Volumes/DISK_C" then
	set diskPath to "/Volumes/DISK_C"
	set loginPart to "?login=User_C:password_C"
	set downloadURL to "https://download.url_C.com"
else
	return
end if

createTinyURL(combineURLs(theSelection, space, diskPath, downloadURL), loginPart)


on combineURLs(theSelection, separator, diskPath, downloadURL)
	if length of separator = 0 then set separator to space
	set theResult to ""
	repeat with theItem in theSelection
		if length of theResult > 0 then set theResult to theResult & separator
		set theResult to theResult & POSIX path of theItem
		set theResult to replaceText(theResult, diskPath, downloadURL) of me
		set theResult to replaceText(theResult, space, "%20") of me
	end repeat
	return theResult
end combineURLs

on replaceText(theText, fromTHIS, toTHIS)
	set ATID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to fromTHIS
	set theResult to text items of theText
	set AppleScript's text item delimiters to toTHIS
	set theResult to theResult as text
	set AppleScript's text item delimiters to ATID
	return theResult
end replaceText

on createTinyURL(theURL, loginPart)
	if length of theURL = 0 then set theURL to (the clipboard as text)
	ignoring case
		if (text 1 thru 4 of theURL) is not "http" then return "invalid URL " & theURL & " (must begin with \"http\")"
		set tinyURL to (do shell script "curl --stderr /dev/null \"http://tinyurl.com/api-create.php?url=" & theURL & loginPart & "\"")
	end ignoring
end createTinyURL

Thanks, you understood it perfectly. Also good for me to see how you split up the script to understand it more. You don’t need an account on TinyUrl, it is just needed to hide some parts of the url (I know this is not safe but it is only for previewing purposes for the client).

So it works good, the only thing that I hadn’t foreseen is the selection of multiple files/folders. In an other script (without the TinyUrl part) I added a linefeed to theResult which gave multiple links on a new line as result. But TinyUrl now makes a complete url of the total result when multiple files/folders are selected, which gives a link that doesn’t work. I tried to add a linefeed in set tinyURL part but that didn’t work, probably because the TinyUrl part has to see If there is a linefeed in the result and then make a new url every time after the linefeed. Is this correct and do you know how to fix this?

I only replaced the set downloadURL to the 3 line as the url stays the same for all volumes. But good to know that this can be changed that way.

Here is the script with that change:

tell application "Finder" to set theSelection to selection as alias list
set control_PosixPath to POSIX path of (item 1 of theSelection)
set downloadURL to "https://download.url.com"

if control_PosixPath begins with "/Volumes/DISK_A" then
	set diskPath to "/Volumes/DISK_A"
	set loginPart to "?login=User_A:password_A"
else if control_PosixPath begins with "/Volumes/DISK_B" then
	set diskPath to "/Volumes/DISK_B"
	set loginPart to "?login=User_B:password_B"
else if control_PosixPath begins with "/Volumes/DISK_C" then
	set diskPath to "/Volumes/DISK_C"
	set loginPart to "?login=User_C:password_C"
else
	return
end if

createTinyURL(combineURLs(theSelection, space, diskPath, downloadURL), loginPart)


on combineURLs(theSelection, separator, diskPath, downloadURL)
	if length of separator = 0 then set separator to space
	set theResult to ""
	repeat with theItem in theSelection
		if length of theResult > 0 then set theResult to theResult & separator
		set theResult to theResult & POSIX path of theItem
		set theResult to replaceText(theResult, diskPath, downloadURL) of me
		set theResult to replaceText(theResult, space, "%20") of me
	end repeat
	return theResult
end combineURLs

on replaceText(theText, fromTHIS, toTHIS)
	set ATID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to fromTHIS
	set theResult to text items of theText
	set AppleScript's text item delimiters to toTHIS
	set theResult to theResult as text
	set AppleScript's text item delimiters to ATID
	return theResult -- & linefeed
end replaceText

on createTinyURL(theURL, loginPart)
	if length of theURL = 0 then set theURL to (the clipboard as text)
	ignoring case
		if (text 1 thru 4 of theURL) is not "http" then return "invalid URL " & theURL & " (must begin with \"http\")"
		set tinyURL to (do shell script "curl --stderr /dev/null \"http://tinyurl.com/api-create.php?url=" & theURL & loginPart & "\"")
	end ignoring
end createTinyURL

Actually, the documentation of the service does not say anything about the possibility of creating a single tiny-url for a list (that is, multiple) of long urls. I just followed your first script assuming it works.

Try creating a list of tiny-urls instead, corresponding to a list of long urls. That is, the result of the script will be a list of tiny-urls instead of a single tiny-url:

tell application "Finder" to set theSelection to selection as alias list
set control_PosixPath to POSIX path of (item 1 of theSelection)

if control_PosixPath begins with "/Volumes/DISK_A" then
	set diskPath to "/Volumes/DISK_A"
	set loginPart to "?login=User_A:password_A"
	set downloadURL to "https://download.url_A.com"
else if control_PosixPath begins with "/Volumes/DISK_B" then
	set diskPath to "/Volumes/DISK_B"
	set loginPart to "?login=User_B:password_B"
	set downloadURL to "https://download.url_B.com"
else if control_PosixPath begins with "/Volumes/DISK_C" then
	set diskPath to "/Volumes/DISK_C"
	set loginPart to "?login=User_C:password_C"
	set downloadURL to "https://download.url_C.com"
else
	return
end if

set tinyURLs to createTinyURLs(theSelection, diskPath, downloadURL)

on createTinyURLs(theSelection, diskPath, downloadURL)
	if length of separator = 0 then set separator to space
	set tinyURLs to {}
	repeat with theItem in theSelection
		set theURL to replaceText(POSIX path of theItem, diskPath, downloadURL) of me
		ignoring case
			if (text 1 thru 4 of theURL) is not "http" then return "invalid URL " & theURL & " (must begin with \"http\")"
			set tinyURL to (do shell script "curl --stderr /dev/null \"http://tinyurl.com/api-create.php?url=" & theURL & loginPart & "\"")
		end ignoring
		set end of tinyURLs to tinyURL
	end repeat
	return tinyURLs
end createTinyURLs

on replaceText(theText, fromTHIS, toTHIS)
	set ATID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to fromTHIS
	set theResult to text items of theText
	set AppleScript's text item delimiters to toTHIS
	set theResult to theResult as text
	set AppleScript's text item delimiters to ATID
	return theResult
end replaceText

The script now gives an error:
error “The variable separator is not defined.” number -2753 from “separator”

Scripteditor says it is this line:

set tinyURLs to createTinyURLs(theSelection, separator, diskPath, downloadURL)

Separator not need at all. I will update the last script.

Thanks, it works almost (not yet for multiple selections), the result in scripteditor returns multiple tinyurls which work if copied from there. The result from scripteditor looks like this {“https://tinyurl.com/xxxxxx”, “https://tinyurl.com/xxxxxx”}
Only when the script is expanded in a text editor or mail programm it returns this <?> For single selection it works fine. Any ideas what could be the issue here?

I also changed the script as the loginPart was defined yet. See the script below:

tell application "Finder" to set theSelection to selection as alias list
set control_PosixPath to POSIX path of (item 1 of theSelection)
set downloadURL to "http://download.url.nl"

if control_PosixPath begins with "/Volumes/DISK_A" then
	set diskPath to "/Volumes/DISK_A"
	set loginPart to "?login=User_A:password_A"
else if control_PosixPath begins with "/Volumes/DISK_B" then
	set diskPath to "/Volumes/DISK_B"
	set loginPart to "?login=User_B:password_B"
else if control_PosixPath begins with "/Volumes/DISK_C" then
	set diskPath to "/Volumes/DISK_C"
	set loginPart to "?login=User_C:password_C"
else
	return
end if

set tinyURLs to createTinyURLs(theSelection, diskPath, downloadURL, loginPart)

on createTinyURLs(theSelection, diskPath, downloadURL, loginPart)
	set tinyURLs to {}
	repeat with theItem in theSelection
		set theURL to replaceText(POSIX path of theItem, diskPath, downloadURL) of me
		ignoring case
			if (text 1 thru 4 of theURL) is not "http" then return "invalid URL " & theURL & " (must begin with \"http\")"
			set tinyURL to (do shell script "curl --stderr /dev/null \"http://tinyurl.com/api-create.php?url=" & theURL & loginPart & "\"")
		end ignoring
		set end of tinyURLs to tinyURL
	end repeat
	return tinyURLs -- & linefeed
end createTinyURLs

on replaceText(theText, fromTHIS, toTHIS)
	set ATID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to fromTHIS
	set theResult to text items of theText
	set AppleScript's text item delimiters to toTHIS
	set theResult to theResult as text
	set AppleScript's text item delimiters to ATID
	return theResult
end replaceText

The result of the script above is a list. To expand it in the TextEdit.app or in the Mail.app, you can covert it to delimited (by linefeed) text:

set tinyURLs to createTinyURLs(theSelection, diskPath, downloadURL, loginPart)

set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set tinyURLs to tinyURLs as text
set AppleScript's text item delimiters to ATID
return tinyURLs

Great, it is working as intended now. Thanks a lot for your help!!

See below a short summary on how I use this script. In case anybody else wants to use it?!

  • load the server share in the finder (the computer of the share functions as FTP)
  • make a selection of a file/folder(s) of which you want to generate a http link
  • I have the script expand in a preformatted email (or some other way to sent the link), it generates a link which can be sent to client
  • it also generates a tinyurl in which the login credentials are stored for auto login (not the safest but fine for our client) You don’t see them anymore when the link is converted in the browser.
  • client will access the file/folder on FTP in my case through a browser environment (the programm I use for this is rumpus)
  • it can be used for multiple shares (with unique login credentials) if needed

Feel free to use it if you need it.

Thanks a lot KniazidisR for helping out with the script!

See final script below:

tell application "Finder" to set theSelection to selection as alias list
set control_PosixPath to POSIX path of (item 1 of theSelection)
set downloadURL to "http://download.url.nl"

if control_PosixPath begins with "/Volumes/DISK_A" then
	set diskPath to "/Volumes/DISK_A"
	set loginPart to "?login=User_A:password_A"
else if control_PosixPath begins with "/Volumes/DISK_B" then
	set diskPath to "/Volumes/DISK_B"
	set loginPart to "?login=User_B:password_B"
else if control_PosixPath begins with "/Volumes/DISK_C" then
	set diskPath to "/Volumes/DISK_C"
	set loginPart to "?login=User_C:password_C"
else
	return
end if

set tinyURLs to createTinyURLs(theSelection, diskPath, downloadURL, loginPart)

on createTinyURLs(theSelection, diskPath, downloadURL, loginPart)
	set tinyURLs to {}
	repeat with theItem in theSelection
		set theURL to replaceText(POSIX path of theItem, diskPath, downloadURL) of me
		ignoring case
			if (text 1 thru 4 of theURL) is not "http" then return "invalid URL " & theURL & " (must begin with \"http\")"
			set tinyURL to (do shell script "curl --stderr /dev/null \"http://tinyurl.com/api-create.php?url=" & theURL & loginPart & "\"")
		end ignoring
		set end of tinyURLs to tinyURL
	end repeat
	set ATID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to linefeed
	set tinyURLs to tinyURLs as text
	set AppleScript's text item delimiters to ATID
	return tinyURLs
end createTinyURLs

on replaceText(theText, fromTHIS, toTHIS)
	set ATID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to fromTHIS
	set theResult to text items of theText
	set AppleScript's text item delimiters to toTHIS
	set theResult to theResult as text
	set AppleScript's text item delimiters to ATID
	return theResult
end replaceText