how to read text files in file made in applescript - shell

How would i read the text the user defined in pass.txt
This is What I have so far to test it you must make a folder on your desktop called test
but I bet most of you could have figured that out
set choice to the button returned of (display dialog "Login or Signup" buttons {"Quit", "Signup", "Login"} default button "Login")
if choice is "Signup" then
set username to the text returned of (display dialog "Enter Desired Username" default answer "")
do shell script "mkdir $HOME/Desktop/test/" & username
do shell script "echo " & username & " > ~/desktop/test/" & username & "/username.txt"
set pass to the text returned of (display dialog "Enter Password" default answer "")
set input to pass
do shell script "echo " & pass & " > ~/desktop/test/" & username & "/pass.txt"
else if choice is "Login" then
set username to the text returned of (display dialog "Enter Username" default answer "")
set y to username
set z to "pass.txt"
set x to "/Desktop/test/" & y
words of {(read (POSIX file x) & "/pass.txt") as «class utf8»} contains pass -- true
set pass to the text returned of (display dialog "Enter Password" default answer "")
else
return 0
end if

The brackets are not set correctly. Try this:
read POSIX file (x & "/pass.txt")
or
do shell script "cat " & quoted form of (POSIX path of (path to desktop) & "test/" & username & "/pass.txt")
There are many other issues in the script anyway. Just a few tips, use...
-quoted form of pathes
-the -p option for mkdir
-the with hidden answer option for password entry

Related

Need help to create gui with buttons to select Shell Scripts via Script editor in MacOs Monterey/Ventura

Im new to creating scripts for Apple (MacOsx Ventura)…
I would like to create an AppleScript with serveral buttons 3 to 9 depending on what Shell script I want to create… What I have now works in Mac terminal with selecting by pressing 1, 2, 3, etc… I like the look of Buttons… this method looks good so… but how to select the option and running a shell script or applescript after clicking ok
set optionList to {“Shell Script-1”, " Shell Script-2", " Applescript-1", " Applescript-2"}
set chosenScript to choose from list optionList with prompt “Choose a Script to run:”
if chosenScript is false then
error number -128 (* User cancelled *)
else
set chosenScript to chosenScript’s item 1 (* extract choice from list *)
end if
display dialog "You chose a " & chosenScript & “!”
With choose from list it's pretty easy to get the selected item by adding index prefices.
set optionList to {"1 Shell Script-1", "2 Shell Script-2", "3 Applescript-1", "4 Applescript-2"}
set chosenScript to choose from list optionList with prompt "Choose a Script to run:"
if chosenScript is false then
error number -128 (* User cancelled *)
else
set chosenIndex to word 1 of (chosenScript's item 1) as integer
end if
display dialog "You chose a " & text 3 thru -1 of item chosenIndex of optionList & "!"
You can even press the index number and then return.
I found what I needed, it maybe not be buttons, but it has selectable options.. I got it with help from ChatGPT
set scriptsList to {"Enable SMB", "Disable SMB", "Create a Local User", "Delete a Local User", "Application Install", "Student Password Fix"}
choose from list scriptsList with title "Script Selection" with prompt "Select a script:"
if the result is not false then
set userChoice to item 1 of result
if userChoice is "Enable SMB" then
set scriptPath to "/Volumes/Ventoy/Scripts/MAC_Scripts/Enable-SMB.zsh"
do shell script "open -a Terminal -sh " & quoted form of scriptPath
else if userChoice is "Disable SMB" then
set scriptPath to "/Volumes/Ventoy/Scripts/MAC_Scripts/Disable-SMB.zsh"
do shell script "open -a Terminal -sh " & quoted form of scriptPath
else if userChoice is "Create a Local User" then
set scriptPath to "/Volumes/Ventoy/Scripts/MAC_Scripts/Create-Local-Account.scpt"
do shell script "osascript " & quoted form of scriptPath
else if userChoice is "Delete a Local User" then
set scriptPath to "/Volumes/Ventoy/Scripts/MAC_Scripts/User-Delete.zsh"
do shell script "open -a Terminal -sh " & quoted form of scriptPath
else if userChoice is "Application Install" then
set scriptPath to "/Volumes/Ventoy/Scripts/MAC_Scripts/Applications-Policies.zsh"
do shell script "open -a Terminal -sh " & quoted form of scriptPath
else if userChoice is "Student Password Fix" then
set scriptPath to "/Volumes/Ventoy/Scripts/MAC_Scripts/Student_Password_Fix.zsh"
do shell script "open -a Terminal -sh " & quoted form of scriptPath
end if
end if

AppleScript that scrapes a website for data with curl function not working after some time making it

The code below used to work at scraping the trading value of a cryptocurrency website. Now it prints the name of the crypto and the time it was read but no value. Is it because the curl function isn't working anymore? Any help is appreciated.
The code uses a curl function to call the value of the crypto every 5 seconds. I got the code from a helpful stack overflow user and it worked well when I implemented it but it doesn't return the monetary value anymore. It creates a log file to the desktop and writes in the background as an application.
property eGrepBitcoinPrice : "priceValue\">\\$\\d{2},\\d{3}.\\d{2}"
property eGrepLitecoinPrice : "priceValue\">\\$\\d{2}.\\d{2}"
property eGrepDogecoinPrice : "priceValue\">\\$\\d{1}.\\d{5}"
property currentBitcoinPrice : missing value
property currentLitecoinPrice : missing value
property currentDogecoinPrice : missing value
property logToTextFile : missing value
on run -- Executed Only Once.. When This Script Applet Is Launched
activate
set logToTextFile to (display dialog ¬
"Enable Quick Log Mode?" buttons {"No", "Yes"} ¬
default button 2 with title "Log Mode")
if button returned of logToTextFile = "Yes" then
my logCommands's beginLog()
getPrices()
else
getPrices()
return {currentBitcoinPrice, currentDogecoinPrice, currentLitecoinPrice}
end if
end run
on idle
getPrices()
try
if button returned of logToTextFile = "Yes" then my logCommands's writeToLog()
on error errMsg number errNum
my logCommands's writeToLog()
end try
(* within this idle handler is where you will place
The bulk of your additional code. All of your Excel
Code Goes Here*)
return 5 -- In Seconds, How Often To Run Code In This Idle Handler
end idle
---------- PLACE ALL ADDITIONAL HANDLERS BENEATH THIS LINE ----------
on getPrices()
set currentBitcoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/bitcoin/markets/' " & ¬
"| grep -Eo " & quoted form of eGrepBitcoinPrice & " | cut -c 14-"
set currentLitecoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/litecoin/' " & ¬
"| grep -Eo " & quoted form of eGrepLitecoinPrice & " | cut -c 14-"
set currentDogecoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/dogecoin/' " & ¬
"| grep -Eo " & quoted form of eGrepDogecoinPrice & " | cut -c 14-"
end getPrices
on quit -- Executed Only When The Script Quits
if button returned of logToTextFile = "Yes" then my logCommands's endLog()
continue quit -- Allows The Script To Quit
end quit
script logCommands
property pathToPriceLog : POSIX path of (path to desktop as text) & "Price Log.txt"
on beginLog()
set startTime to ("Start Time... " & (current date) as text) & ¬
" Price Scanning At 5 Minute Intervals"
do shell script "echo " & startTime & " >> " & ¬
quoted form of pathToPriceLog
end beginLog
on writeToLog()
do shell script "echo " & "Bitcoin:" & quoted form of currentBitcoinPrice & ¬
" Dogecoin:" & quoted form of currentDogecoinPrice & ¬
" Litecoin:" & quoted form of currentLitecoinPrice & ¬
" " & quoted form of (time string of (current date)) & ¬
" >> " & quoted form of pathToPriceLog
end writeToLog
on endLog()
set endTime to quoted form of "End Time... " & (current date) as text
do shell script "echo " & endTime & " >> " & ¬
quoted form of pathToPriceLog
do shell script "echo " & " " & " >> " & ¬
quoted form of pathToPriceLog
end endLog
end script

How to save your desktop elements (icons, files,folder ) always on the same place with applescript

With the script we are going to create another script where will be store position of all the elements of the desktop, the created script will be compile and usable to put back in place all the elements previously protected.
/adesktopsave/deskico.txt it is the temporary file which will be of use to the compilation.
/adesktopsave/savedicoposition.scpt It is the script of saving that is compiled to be used with applescrit
All the names used here exist that just for the example. These names have no particular property.
It is just necessary to plan to create a folder before using this script. Here it is:
/adesktopsave
Something else, end of line (\n) after " try
"
also " end try
"
and & "}
")
Are very important to respect so that the text is usable.
tell application "Finder" to set theList to {name, desktop position} of items of desktop
try
do shell script "rm -f /adesktopsave/deskico.txt"
do shell script "echo tell application " & quoted form of (quote & "Finder" & quote) & return & " >>/adesktopsave/deskico.txt"
end try
set n to (count (first item of theList))
repeat with i from 1 to n
set inp to do shell script "echo " & quoted form of (item i of first item of theList)
set xy to (item i of second item of theList)
set AppleScript's text item delimiters to ","
set xyz to do shell script "echo " & xy
set wxyz to ("{" & xyz & "}
")
set ligne to "try
" & "set desktop position of item " & quoted form of (quote & inp & quote) & " of desktop to " & quoted form of (wxyz) & "end try
"
set ligne to do shell script "echo " & ligne & " >>/adesktopsave/deskico.txt"
end repeat
do shell script "echo " & "end tell" & return & " >>/adesktopsave/deskico.txt"
display dialog "Do you want to save your icons in their current location?" buttons {"Cancel", "Save"} default button 2 with title "Save the positions of icons"
if (button returned of result) is "Cancel" then
set n to do shell script "echo " & n
else
do shell script "osacompile -o " & "/adesktopsave/savedicoposition.scpt" & " /adesktopsave/deskico.txt"
end if
return n
We can lighten the script to its simplest expression. At the risk of having errors can be.
set ligne to ""
do shell script "mkdir -p /adesktopsave"
tell application "Finder" to set {names, positions} to {name, desktop position} of items of the desktop
set ligne to "tell application \"Finder\"
"
set n to (count names)
set AppleScript's text item delimiters to ","
repeat with i from 1 to n
set ligne to ligne & ("try
" & "set desktop position of item " & (quote & item i of names & quote) & " to {" & item i of positions & "}
end try
")
end repeat
set ligne to ligne & ("end tell" & return)
display dialog "Do you want to save your icons in their current location?" buttons {"Cancel", "Save"} default button 2 with title "Save the positions of icons"
if (button returned of result) is "Cancel" then
set n to do shell script "echo " & n
else
do shell script "osacompile -o " & "/adesktopsave/savedicoposition.scpt -e " & quoted form of ligne
end if
set AppleScript's text item delimiters to ""
tell application "Finder" to open POSIX file "/adesktopsave/savedicoposition.scpt"
return n

Try statements acting up in AppleScript

I'm trying to figure out try statements in AppleScript. I got it to compile, but it doesn't do what I want it to do.
try
do shell script "cat " & passData
on error
display dialog "Create a password to protect this file. After creating the password, you will need to enter it every time in order to access the app." default answer "" buttons {"Cancel", "OK"} default button 2 with hidden answer
set pass1 to text returned of result
display dialog "Confirm the password you entered." default answer "" buttons {"Cancel", "OK"} default button 2 with hidden answer
set pass2 to text returned of result
if pass1 is pass2 then
set setPass to pass2
set passSalt to random number from 10000000 to 99999999
do shell script "echo " & setPass & " | shasum -a 512 | awk '{print $1}'"
set passHash to result
set cHash1 to random number from 10 to 25
set cHash2 to random number from 26 to 50
set cHash3 to random number from 51 to 75
set cHash4 to random number from 76 to 99
set hBlock1 to characters cHash1 through cHash2 of passHash
set hBlock2 to characters cHash3 through cHash4 of passHash
set finalHash to hBlock1 & passHash & hBlock2
do shell script "echo " & passSalt & return & cHash1 & return & cHash2 & return & cHash3 & return & cHash4 & " > " & passData
do shell script "echo " & finalHash & " > " & appPath & "Contents/Resources/FinalHash.txt"
else
display dialog "The passwords didn't match. Restart the application and try again." buttons {"OK"} default button 1
quit
end if
quit
end try
I want the application to quit if the text entered in the first and second dialog boxes don't match, but it just goes on to the rest of the code. I also want the application to quit after it does all the hashing mumbo jumbo if the text entered in the boxes match, but it doesn't do that either. How can I fix this? I suspect it has something to do with the try statements.
Edit: when I said it goes on to the rest of the code, I meant when I exported it as an app. It works correctly in the editor but in the app it continues to the rest of the code.
You put an end try on the end of your script, but there's no opening try to close off.
In the code below I've removed the end try. In addition to that, I've added tell application "finder" and end tell to prevent your script editor from quiting.
Tried it and it works fine here.
tell application "Finder"
display dialog "Create a password to protect this file. After creating the password, you will need to enter it every time in order to access the app." default answer "" buttons {"Cancel", "OK"} default button 2 with hidden answer
set pass1 to text returned of result
display dialog "Confirm the password you entered." default answer "" buttons {"Cancel", "OK"} default button 2 with hidden answer
set pass2 to text returned of result
if pass1 is pass2 then
set setPass to pass2
set passSalt to random number from 10000000 to 99999999
do shell script "echo " & setPass & " | shasum -a 512 | awk '{print $1}'"
set passHash to result
set cHash1 to random number from 10 to 25
set cHash2 to random number from 26 to 50
set cHash3 to random number from 51 to 75
set cHash4 to random number from 76 to 99
set hBlock1 to characters cHash1 through cHash2 of passHash
set hBlock2 to characters cHash3 through cHash4 of passHash
set finalHash to hBlock1 & passHash & hBlock2
do shell script "echo " & passSalt & return & cHash1 & return & cHash2 & return & cHash3 & return & cHash4 & " > " & passData
do shell script "echo " & finalHash & " > " & appPath & "Contents/Resources/FinalHash.txt"
else
display dialog "The passwords didn't match. Restart the application and try again." buttons {"OK"} default button 1
quit
end if
end tell
Never mind, I found out a way that works! :)
Between the try and on error, I added set quitFunction to "0". Then between the on error and end try I added set quitFunction to 1. After the end try I made the script test if the variable quitFunction is 1. If it is, it quits and if it isn't, it doesn't quit.

How to put the captured screenshot into folder with the Applescript?

am trying to capture screenshot and dump into folder on the desktop with the applescript..
i was successful in taking the screenshot but not dumping into folder which is not existing on the desktop.. Pls suggest..
I tried..
set loc to "/Users/username/Desktop/New Folder"
property N : 0
set N to N + 1
set picPath to (loc & "Picture_" & N & ".png") as string
do shell script "screencapture -tjpg " & quoted form of picPath
After the first line (set loc...) add the following line:
do shell script "mkdir -p " & quoted form of loc

Resources