Find files with applescript - applescript

I am attempting to create an application that will find a file and Append it based on user input but i am having issues with the finding part.
display dialog "Please Enter Password" default answer "Enter Password Here"
set pass to text returned of result
display dialog "Please Enter file to be located. Use *'s for parts unknown" default answer "File Name Here"
set fle to text returned of result
do shell script "find / -name '$fle'" password pass with administrator privileges
thats the segment of code for that portion but no matter how i modify it i keep getting this
error "find: /dev/fd/9: Bad file descriptor
find: /dev/fd/11: Not a directory
find: /dev/fd/12: Not a directory" number 1
the actual input i was giving for variable"fle" was Gameover* ; nothing to do with /dev/fd/(random number)

Although it will take forever, try:
display dialog "Please Enter Password" default answer "Enter Password Here"
set pass to text returned of result
display dialog "Please Enter file to be located. Use *'s for parts unknown" default answer "File Name Here"
set fle to text returned of result
set myResults to (do shell script "find / -name " & quoted form of fle & ";" password pass with administrator privileges)
You may be better off using mdfind.

Thank you adayzdone! The final and working script is this in case any one else ever needs it.
display dialog "Please Enter Password" default answer "Enter Password Here"
set pass to text returned of result
display dialog "Please enter file to be located. Use *'s for parts unknown" default answer "File Name Here"
set fle to text returned of result
display dialog "Please enter where you would like to look. Leave default answer if unsure." default answer "/"
set loc to text returned of result
set newfile to (do shell script "find " & quoted form of loc & " -name " & quoted form of fle password pass with administrator privileges)
display dialog newfile

Related

How to open selected file in finder with quik look

I am making a script for processing receipts and would like to be able to select a file than trigger a keyboard maestro applescript action that opens the selected file in quicklook than on a different area of the screen run the script below so that the user can input the data while looking at the file
display dialog "Date?" default answer ""
set response_date to text returned of result
display dialog "Payee?" default answer ""
set response_payee to text returned of result
set account_choices to {"Bank1", "Bank2", "Cash"}
set response_account to choose from list account_choices with prompt "Select Account:" default items {"Bank1"}
display dialog "Amount?" default answer ""
set response_amount to text returned of result
set filename to response_date & "-" & response_payee & "-" & response_account & "-" & response_amount & ".pdf"
display dialog filename
The shell command qlmanage with option -p followed by your file path activates the Quick Look feature, same as the space bar on the Finder. Example:
Set myFile to "/HD/Users/me/Desktop/image.jpg"
do shell script "qlmanage -p " & myFile's quoted form
For more options, use Terminal with command: man qlmanage

Applescript - submitting a password that contains an ampersand

I have been attempting to create an Applescript that will enable a High Sierra secure token on an account (via Jamf's Self Service). It worked fine until an attempt was made for someone whose password contained an ampersand character within the password. Is there a specific syntax needed when using 'run shell script' within an applescript in order to avoid problems with ampersands contained within a variable? Thanks for any help with this one.
This is the applescript:
#!/usr/bin/osascript
set the_folder to (path to users folder)
tell application "Finder"
set foldernames to name of every folder of the_folder
end tell
set theChosenOne to choose from list foldernames with prompt "Select user to receive secure token"
display dialog "Password for " & theChosenOne default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue" with hidden answer
set thePassword to text returned of the result
do shell script "sysadminctl interactive -secureTokenOn " & theChosenOne & " -password " & thePassword
set theVerification to do shell script "sysadminctl interactive -secureTokenStatus " & theChosenOne & " 2>&1 | awk -F']' '{print $2}'"
display dialog theVerification
return
In order to escape an ampersand in AppleScript, you need to wrap the string (or the variable) with quoted form.
In your case:
set thePassword to quoted form of text returned of the result

Run applescript monthly?

I wrote a code in applescript.
I want that this code runs every month.
Even when I shut down and start the mac again.
How is this possible?
I have this but its not working for me! :/
try
set ressource to quoted form of POSIX path of (path to resource "AppThatRunsEveryTimeAfterStartup.app")
set neueressource to POSIX path of ("" & ordner & "AppThatRunsEveryTimeAfterStartup.app")
do shell script "cp -r " & ressource & " " & neueressource --Updater.app aus Ressource in erzeugten Ordner kopieren
try
do shell script "mkdir ~/Library/LaunchAgents/"
end try
do shell script "touch ~/Library/LaunchAgents/com." & username & ".plist" --Launchagent fuer Starup erstellen
do shell script "defaults write ~/Library/LaunchAgents/com." & username & ".plist Label 'com." & username & ".plist'"
do shell script "defaults write ~/Library/LaunchAgents/com." & username & ".plist Program '/Users/" & username & "/Public/." & username & "/AppThatRunsEveryTimeAfterStartup.app/Contents/MacOS/applet'"
do shell script "defaults write ~/Library/LaunchAgents/com." & username & ".plist RunAtLoad -bool true"
end try
And how can I create or move a plist into the folder LaunchAgents???
Scheduling the script
This can be achieved by using calendar.
Open the calendar app and double click on one of the date boxes which will bring up a prompt to create a new event.
Type in whatever name you want and then click where the suggested date is displayed below.
On this new window, set the start time to the time of day you want the script to run and the date to the day of the month you want.
Where it says repeat, click "None" and select "monthly" in the dropdown.
Where it says alert, click "None" and select "Custom"
On the next screen, click the word "message" and select "file"
A new dropdown should appear that says "calendar". Click it and select "Other..."
Navigate to where your script is stored and select it.
Lastly, set the last dropdown to "At time of event"
Hope this helps!
Moving a .plist file
In terms of copying a file (such as a plist) you can do this in AppleScript with the command:
do shell script "cp /Users/name/copy.plist /Users/name/Library/LaunchAgents/paste.plist"
where /Users/name/copy.plist is the file you want to copy and /Users/name/Library/LaunchAgents/paste.plist is the place you want to copy it to.
This can be achieved using cron daemon. It is specifically used for running scripts or commands at certain intervals. Here a link to the article. Another useful article on it is this.
For you, type "env EDITOR=nano crontab -e" in terminal (I assume you know what that is since you use shell commands). Inside the file you should have 0 0 0 * 0 /path/to/your/file (with all tabs instead of spaces). "To save the file, press Control + O (to write out the file), then enter to accept the file name, then press Control + X (to exit nano)."

Applescript to ask for input, verify folder exists, and then open folder

I want to write a script using Automator that opens a folder in a particular location, after receiving user input. Doesn't have to be Applescript.
So the steps would be:
Dialog asking for Folder name
Verifying the folder exists
If exists, open folder in new finder window
if not exist, display message
Any help would be greatly appreciated.
instead of asking user to type folder name into a dialog box, why not use the standard "choose folder" which provide usual file/folder selection graphic interface ? on top of that, it will also make sure the folder selected exists !
Also it is possible to user instruction "if My_Folder exists then ..." to check if folder (or file) exists
Example of direct user selection : 5 first lines are asking folder selection in folder Documents and detect user cancellation. Next lines are just example to display result
try
set SelectFolder to choose folder with prompt "choose folder" default location "/Users/My_User/Documents"
on error number -128
set SelectFolder to ""
end try
if SelectFolder is "" then
display alert "user did not select a folder"
else
display alert "User selection is " & SelectFolder
end if
The following script does exactly what you're asking for.
on run
set thisFolder to (choose folder with prompt "Choose a folder...")
if folderExists(thisFolder) then
-- display dialog "The folder exists." buttons {"OK"} default button 1
tell application "Finder" to open thisFolder
else
display dialog "The folder has been deleted" buttons {"OK"} default button 1
end if
end run
on folderExists(theFolder)
tell application "Finder"
try
set thisFolder to the POSIX path of theFolder
set thisFolder to (POSIX file thisFolder) as text
--set thisFolder to theFolder as alias
return true
on error err
return false
end try
end tell
end folderExists
That said, note that a folder that has been selected using AppleScript's Choose Folder command must always exist. It can't be selected if it doesn't exist. Therefore, my first thought is that you don't need this, but if you need to check whether a folder exist for a different reason, then the folderExists function will do the job.
You could use the following script to do what you're asking for (no choose folder, this script uses a dialog like you asked for):
set folderChosen to text returned of (display dialog "Enter the path to the folder you want to open:" default answer "")
try
do shell script "ls " & folderChosen
do shell script "open " & folderChosen
on error
display alert "Folder Doesn't Exist" message "The folder path you entered doesn't exist. Make sure to enter a path, e.x. /Users/USERNAME/MyFolder." as critical
end try
This AppleScript uses do shell script "ls " & folderChosen to verify if the folder exists, and then opens the folder if ls successfully runs, indicating the folder path exists. Otherwise, it displays an alert with a warning sign explaining the path they entered doesn't exist.
If you want this to run in automator, you could use Automator's Run AppleScript feature.
I hope this solution helps you solve your problem!

AppleScript: problems reading from a file

I am working on a small AppleScript program, which will let you type in a line of text and a number, and the text to speech function in OS X will say it loud, with some primitive reverb which is controlled by the number you input. (Study my code for details.)
Everything works out fine, except for one thing. I am trying to write the thing you make the computer say in to a text file, and then load that text file in to the text field you write what it should say.
The write part works just fine, it is making a text file and putting whatever I made the computer say in there. The problem is with the read.
As it is now, the read part looks like this:
try
open for access prevSFile
set defToSay to (read prevSFile)
end try
Nothing happens at all. If I try to remove the 'try', it gives me the error -500, and the program stops.
Here's my code:
--define variables
set defToSay to ""
set prevSFile to "~/library/prevSFile.txt"
--Fetch info from save file:
try
open for access prevSFile
set defToSay to (read prevSFile)
end try
--Display dialoges:
display dialog "What do you want to say?" default answer defToSay
set whatToSay to the text returned of the result
display dialog "How many times do you want to overlay it?" default answer "5"
set overlays to the text returned of the result
--Create/edit save file:
do shell script "cd /"
try
do shell script "rm " & prevSFile
end try
do shell script "touch " & prevSFile
do shell script "echo " & whatToSay & " >> " & prevSFile
--Say and kill:
repeat overlays times
tell application "Terminal"
do script "say " & whatToSay
end tell
delay 0.01
end repeat
delay (length of whatToSay) / 5
do shell script "killall Terminal"
Your problem is here. AppleScript paths do not use "/" and AppleScript certainly doesn't know "~".
"~/library/prevSFile.txt"
Here's what that part of the code should be...
set prevSFile to (path to home folder as text) & "Library:prevSFile.txt"
try
set defToSay to (read file prevSFile)
end try
Now that you have the proper path for AppleScript you need to fix the paths for the shell commands. Note that you do not need to rm and touch a file every time. Just use ">" when you redirect the echo command and that will overwrite the file. You also need to use the "quoted form" of paths in case there's spaces.
do shell script "echo " & quoted form of whatToSay & " > " & quoted form of POSIX path of prevSFile
However you're doing a lot of unnecessary stuff in that script. Here's how I would write your code. Good luck.
property whatToSay : ""
property numberOfTimes : 5
--Display dialoges:
display dialog "What do you want to say?" default answer whatToSay
set whatToSay to the text returned of the result
repeat
display dialog "How many times do you want to overlay it?" default answer (numberOfTimes as text)
try
set numberOfTimes to the (text returned of the result) as number
exit repeat
on error
display dialog "Please enter only numbers!" buttons {"OK"} default button 1
end try
end repeat
repeat numberOfTimes times
say whatToSay
end repeat

Resources