Mac Contextual Menu for AFP Path Without Spaces - macos

My team requires an Automator service that will allow us to copy file paths from within OS X Finder such that they always start with "afp://server-name/" instead of "/Volumes/server-name/" BUT ALSO replaces all spaces with "%20".
Right now, we are using the following code, which does replace the first space in a given string, but does not replace all spaces.
on run {input, parameters}
set output to {}
repeat with f in input
set end of output to replace(POSIX path of f, "/Volumes/Brand Design", "afp://nycp-afp01/Brand%20Design")
end repeat
set text item delimiters to linefeed
set the clipboard to (output as text)
end run
on replace(input, search, replace)
set text item delimiters to search
set ti to text items of input
set text item delimiters to replace
ti as text
end replace
Any help you can provide will be HUGELY appreciated; thanx!
A.

to get rid of spaces use your replace handler again Inside itself (last line).
my replace(ti as text, " ","%20")

Related

How do I prevent zsh from replacing "%5D" with "]"?

[EDIT] Solved.
The actual issue was a typo in one of the handler variables, and not a zsh issue, as I initially thought.
I am trying to prevent zsh from replacing the code "%5D" for the ] right square bracket with the actual character "]".
For my purposes - calling the last.fm API via curl in shell script, to get info about a track from Apple Music - it needs to stay as "%5D", because otherwise the API doesn't recognise it.
I have tried escaping the "%5D" with backslashes or a second "%" but that doesn't work, it still gets replaced with "]"
Here's an example of what I'm trying to do and what is not working:
The original string of the track's name:
"YESTODAY (Extended Version) [Bonus Track]"
How it needs to be spelled for the API to work:
"YESTODAY+(Extended+Version)+%5BBonus+Track%5D"
The whitespaces get replaced with "+" by my AppleScript handler.
The left [ stays as "%5B" (I guess because it is immediately followed by more letters and therefore zsh cannot recognise it as code and replace it).
The right ] is generally the last character of the track string, not followed by anything and thus "%5D" is recognised by zsh and written as "]" .
How do I fix this?
Any help is greatly appreciated :)
For reference:
The part of the AppleScript that is supposed to replace the "[ ]" with code.
(This is obviously not the complete script). If you run this, it replaces the [ ] correctly. The issue only arises once the entire API call is made with do shell script curl_command because of zsh interpreting the code.
set trackreplace to "YESTODAY (Extended Version) [Bonus Track]"
if trackreplace contains "[" then
set trackreplace to replaceChars("[", "%5B", trackreplace)
end if
if trackreplace contains "]" then
set trackeplace to replaceChars("]", "%5D", trackreplace)
end if
on replaceChars(find, replace, subject)
set savedelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to find
set subject to text items of subject
set AppleScript's text item delimiters to replace
set subject to (subject as string)
set AppleScript's text item delimiters to savedelims
return subject
end replaceChars
It was not a zsh issue but actually just a typo in one of the handler variables (trackeplace instead of trackreplace), as pointed out by #GordonDavisson

Applescript to remove all text not between two strings

I'm trying to make a script that displays a dialog of the current iTunes Top 20. How I intend to do this is get the html code from the top 100 website and then extract for text between two strings to get the name of the song. For the first song, this is extremely successful.
However, it only works for the first song each time. The only way I can think of to fix this is rather then get everything between the two strings, I could delete everything not between them. This would hopefully then give me all the song names as a string.
Does anyone know how to do this?
Get the HTML:
set curlcommand to "curl https://www.apple.com/itunes/charts/songs/"
set html to (do shell script curlcommand)
Get the song name:
set AppleScript's text item delimiters to "width=\"100\" height=\"100\" alt=\""
set theText to item 2 of every text item of html
set AppleScript's text item delimiters to "\"></a>"
set theText to item 1 of every text item of theText
set AppleScript's text item delimiters to ""
You could use the shell to get a list with all song names line by line (this is not a AppleScript list).
set charts to (do shell script "
curl -s 'https://www.apple.com/itunes/charts/songs/'| tr '\"' '\n' |awk '/^ alt=$/ {getline;print}'")
tr '\"' '\n' substitutes the double quotes with new lines (tr manual page)
awk '/^ alt=$/ {getline;print}' prints the line after the line alt= where the song name is written (awk manual page)

Copy file paths to clipboard in OSX doesn't work with files on a server...workaround not working with multiple files

I'm using automator to achieve this. Here's the applescript code I'm using (found elsewhere on here):
get the clipboard
set the clipboard to (replacement of "/Volumes/" by "afp://servername.com/" for the result)
on replacement of oldDelim by newDelim for sourceString
set oldTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to oldDelim
set strtoks to text items of sourceString
set text item delimiters of AppleScript to newDelim
set joinedString to strtoks as string
set text item delimiters of AppleScript to oldTIDs
joinedString
end replacement
get the clipboard
set the clipboard to (replacement of "/Volumes/" by "afp://servername.com/" for the result)
This works perfectly unless I select multiple files. Can anyone help? This code seems to fail when the input is multiple file paths.
You might use workflows like this:
on run {input, parameters}
set output to {}
repeat with f in input
set end of output to replace(POSIX path of f, "/Volumes/", "afp://servername.com/")
end repeat
set text item delimiters to linefeed
set the clipboard to (output as text)
end run
on replace(input, search, replace)
set text item delimiters to search
set ti to text items of input
set text item delimiters to replace
ti as text
end replace
sed 's|/Volumes/|afp://servername.com/|'|LC_CTYPE=UTF-8 pbcopy

get language of currently logged in user via applescript

I want to get the system language of the currently logged in user. The line
set lang to do shell script "defaults read NSGlobalDomain AppleLanguages"
returns an string, which looks like
(
en,
de,
ja,
fr,
es,
it,
pt,
"pt-PT",
nl,
sv,
nb,
da,
fi,
ru,
pl,
"zh-Hans",
"zh-Hant",
ko,
ar,
cs,
hu,
tr
)
returns the users languages, but how can I get the first one of this 'array'? Is there a possibility to parse this as an array an get its first value?
There's a more direct method. Applescript has a command "system info" which returns a lot of useful information about the current user. Try this to see...
return system info
The information that will help you from that is "user locale". So you can get the language easily...
return user locale of (get system info)
Play around with this on different users and see if it gives you what you want.
The thread is really old, but I just had the problem and this comes up pretty high in a Google search. For other readers, I want to add two comments:
There is a difference between the “return user locale of (get system info)” and the “defaults read NSGlobalDomain AppleLanguages” strategies: The first returns the value from the region settings, the latter the value from the language settings (both in “Language & Text”).
Region settings return a language and a region, separated by underscore. Language settings return either just a language or language and region, separated by hyphen. If you use that in other code, make sure, it is tolerant.
The second point: GREPing works, but the simplest code I found is using the Property List suite from System Events:
on get_language()
set lang to do shell script "defaults read NSGlobalDomain AppleLanguages"
tell application "System Events"
set pl to make new property list item with properties {text:lang}
set r to value of pl
end tell
return item 1 of r
end get_language
grep my be faster, but this requires less brain twisting.
Jürgen
You can use awk and grep to prepare the list a little (get rid of indentation, quotes and parentheses), then split the resulting string:
-- a standard split function
to split of aString by sep
local aList, delims
tell AppleScript
set delims to text item delimiters
set text item delimiters to sep
set aList to text items of aString
set text item delimiters to delims
end tell
return aList
end split
-- pipe the output of defaults through a few more commands
set cmd to "defaults read NSGlobalDomain AppleLanguages | awk '{gsub(/[^a-zA-Z-]/,\"\");print}' | grep -v '^$'"
set langs to do shell script cmd
-- get the first item in the list
set lang to item 1 of (split of langs by return)
The shell command gives you a list like:
en
da
ja
fr
de
es
it
pt
pt-PT
nl
sv
nb
fi
ru
pl
zh-Hans
zh-Hant
ko
So item 1 will be en
By now this thread is ancient, but still the most relevant one of all results to my search entry. So I think this may be relevant to others searching for an answer to this question.
Based on what user1635960 wrote, the following “one liner” appears quite simple to me:
set lang to first word of (do shell script "defaults read NSGlobalDomain AppleLanguages")
As my first system language is German, it simply returns de.
Explanation:
do shell script "defaults read NSGlobalDomain AppleLanguages"
returns a list like
"(
\"de-DE\",
\"nl-DE\",
\"en-DE\"
)"
The class word defines a sequence of mainly letters and/or numbers, delimited by one or more of most other characters. So first word of the above list results in de.

How to pass variables from ruby script into automator app?

So I have a script in ruby that obviously uses variables. Part of the script opens an app and runs an automator workflow. There are some variables in my ruby script that I need my workflow to use. Is this possible in anyway?
Are you able to:
a) Execute a command line program from Ruby
b) Save your Automator workflow as an application
If so, you should be able to run the open command, e.g. open test.app --args someArg. Or you could use the automator command, such as echo "someArg" | automator -i - test.app
Note that the entire Automator script will run once for each argument - try having ‘Speak Text’ as your first item to verify this.
To work with all the parameters in one go you need to actually pass just one and then split it up, e.g. open test.app --args "one|two|three|four" then something like
on run input
set myArray to my theSplit(input as string, "|")
set a to item 1 of myArray
set b to item 2 of myArray
set c to item 3 of myArray
set d to item 4 of myArray
display dialog "c is " & c
--do stuff
return str
end run
on theSplit(theString, theDelimiter)
-- save delimiters to restore old settings
set oldDelimiters to AppleScript's text item delimiters
-- set delimiters to delimiter to be used
set AppleScript's text item delimiters to theDelimiter
-- create the array
--set theArray to every text item of theString
set theArray to text items of theString
--display dialog theArray as string
-- restore the old setting
set AppleScript's text item delimiters to oldDelimiters
-- return the result
return theArray
end theSplit
However, the AppleScript only seems to work if it’s not the first action. If you need it as the first action, which you probably do, insert a Run Shell Script first that just passes on the arguments:
for f
do
echo “$f"
done
Yes, it is possible to pass parameters to a ruby script. Here is a nice tutorial:
http://ruby.about.com/od/rubyfeatures/a/argv.htm

Resources