setting text to clipboard in applescript - macos

Whenever i try to run the following code:
set RAccounts to (read POSIX file "/Users/student/Desktop/Accounts.txt")
set word 1 of RAccounts to clipboard
It gives me the error " cannot set text to clipboard " any help without having to do keystrokes etc. also when i try
get word 1 of RAccounts and paste
it tells me cannot make into type boolean any help guys?

If you are you trying to put word 1 into the clipboard.
set the clipboard to word 1 of RAccounts

Related

Mac Contextual Menu for AFP Path Without Spaces

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")

need to return "\" not "\\" in applescript to build bash command

Thanks for the help.
I'm trying to build a series of bash commands using Applescript. My problem is applescript using \ as an escape character. so when it returns my strings its returning \\ not \ as needed. Has anyone found a way to return a string with a single \?
Also, previous help I've received on this people will say "if you display dialog it will say \ not \\" which is correct however I'm not attempting to display text I need to pass these strings on to bash.
--get all the finder info
tell application "Finder"
set selCnt to selection as list
if (count of items in selCnt) is equal to 1 then
set theDir to the selection as alias
set theDir to POSIX path of theDir
else if ((count of items in selCnt) is greater than 1) then
display dialog "Please select only one directory. ie: 'Documents'"
else if (count of items in selCnt) is equal to 0 then
set theDir to target of window 1 as alias
set theDir to POSIX path of theDir
else
display dialog "What do you think you're doing?"
end if
end tell
--rename strings with spaces
set t to theDir
set t to tid(t, " ") -- converts to a list of text items
set t to tid(t, "\\ ") -- converts back to tex a list odf text items
on tid(input, delim)
set {oldTID, my text item delimiters} to {my text item delimiters, delim}
if class of input is list then
set outputA to input as text
else
set outputA to text items of input
end if
set my text item delimiters to oldTID
return outputA
end tid
set theDir to result
--strings setup
set cdStr to "cd " & theDir
set stringA to ".doc"
set stringB to "\\"
set delDoc to "find . -name " & quoted form of stringA & " -exec rm {} " & stringB & ":"
set stringA to ".exl"
set stringB to "\\"
set delExl to "find . -name " & quoted form of stringA & " -exec rm {} " & stringB & ":"
--execute shell scripts
set theCmd to cdStr & return & delDoc & return & delExl
return theCmd
(*
do shell script cdStr
do shell script delExl
do shell script delDoc
*)
You simply need to escape a backslash with a backslash to pass on that string.
set foo to "\\"
will set foo to a single "\". You can verify this using any of the following:
display dialog foo
log foo
return foo
do shell script "echo \\$foo"
Run this last command, and it returns "$foo", proving that the shell command correctly received a single slash, which in this instance tells the shell command to ignore the special significance of $. If it had sent two slashes to the shell command, you would've gotten an error, or possibly a literal slash.
You can psych yourself out by getting confused by your editor's display of "\", but the results end up working.
You don't state in your question what is making you think it's not behaving correctly. What does "it freaks out" mean?
I was able to figure everything out using bash commands mostly. Then I was able to create a service which ran when i hit a certain key set like "cmd + option + R." All the user has to do is select the folder and hit the keys and the program will erase all of the files with the specific extension. step 2 from my notes below allow bash to recognize the source which is determined by the first open window of Finder.
Again sorry for all the formatting trouble this is only my second ever post in stackoverflow
first modify the .bash_profile. Open it via terminal
open ~/.bash_profile -a TextEdit
copy and past script into the txt file then save. This allows you to now issue a command “cdf”
cdf () {
theFinderPath=osascript <<END
tell application "Finder"
try
set thePath to get the POSIX path of (folder of the front window as string)
on error
set thePath to get the POSIX path of (path to desktop folder as string)
end try
end tell
END
cd "$theFinderPath";
}
rewrite script to include new command and then save this to the root of your “Documents” folder.
3.
! /bin/bash
cd to the path of the front Finder window
cdf
find . -name ‘.doc’ -exec rm {} \; #deletes all doc files
find . -name ‘.exe’ -exec rm {} \; #deletes all exe files
Now using Automator you can write a simple code that will issue this command from a keystroke.
bash -x /Users/username/Documents/sh/script.sh || true

Applescript errors when reading user input (password) that contains escape characters

Here in my department we use a samba shares server as storage space for employees. For the employees that use Apple machines, we are trying to use an AppleScript application that will obtain the users username and password, then using them to mount the drives. The problem arises when a user enters a password that contains escape characters; for example abcdefg/. Here is the dialog code that sets the variables for username and password:
set user_name_dialog to display dialog "Enter your User ID: " default answer "" buttons {"Next"} default button "Next"
set user_name to text returned of user_name_dialog
--this line prompts the user for their password and sets the returned result as the variable "user_password"
set user_password_dialog to display dialog "Enter your Password. " & return & return & "WARNING: If you are running Panther (MacOS 10.3), your input will be displayed in this box as clear text." default answer "" buttons {"Next"} default button "Next" with hidden answer
set user_password to text returned of user_password_dialog
And here is the shell script line that is ran for mounting a drive:
do shell script "mount -t smbfs //" & user_name & ":" & user_password & "#SOME IP ADDRESS/SOME FOLDER/ /SOME MOUNT POINT/" & user_name & "-SOME SHARE"
Now I know from a little research that it could be something as simple as an extra pair of quotes somewhere, I've even tried
quoted form of
with no luck whatsoever. The error message I get when entering a string with a escape character is as such:
error "mount_smbfs: server connection failed: No route to host" number 68
Any help would be GREATLY appreciated as my experience with AppleScript is very limited.
NOTE: the following is just a guess. I did not test any of it.
I don't believe it is an "escape characters" problem. The forward slash is not an escape character. The backward slash is. The problem is probably that the smbfs command interprets the forward slash as a divisor for path variables, and that's causing the error. So the question becomes... how can you avoid the conflict between the forward slash and the smbfs command?
I would try escaping the forward slash when you pass the command to the shell. Note that applescript also uses a backslash as an escape character so to pass an escape character to the shell you will have to escape the escape. So write a custom handler to pass your passwords through to perform that escape process for you.
So if this is your password, "abcdefg/" then try passing it as "abcdefg\\/" (note the double backslash). As mentioned you can write a custom handler to check your password for the "/" character, and if found then insert "\\" in front of it. But before writing that handler try the proposed solution manually to see if it solves your problem.
set pword to "/abc/defg/"
escapeSlash(pword)
on escapeSlash(x)
if x contains "/" then
set AppleScript's text item delimiters to "/"
set textItems to text items of x
set AppleScript's text item delimiters to "\\/"
set x to textItems as text
set AppleScript's text item delimiters to ""
end if
return x
end escapeSlash

plain text URL to HTML code (Automator/AppleScript)

Suppose I have a plain txt file in a text editor such as TextEdit:
title 1
http://a.b/c
title 2
http://d.e/f
...
I'd like to convert all the lines beginning with http:// to HTML code for URL, so that the aforementioned content will become:
title 1
http://a.b/c
title 2
http://d.e/f
...
How can I get this done in Automator or AppleScript? (My current solution is using Gmail, but it involves multi-step copy-paste.)
Thank you very much in advance.
This will let you avoid another editor:
set inFile to "/Users/you/Desktop/Urls.txt"
set outFile to "/Users/you/Desktop/Urls2.txt"
do shell script "sed 's/\\(http[^ ]*\\)/<a href=\"\\1\">\\1<\\/a>/g' " & quoted form of inFile & " >" & quoted form of outFile
Just do a regex search and replace in a text editor or Terminal:
sed -E 's|^(http:.*)|\1|g' file.txt

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