Search code examples
macosbashvariablesfindtemporary-files

Save variable RESULTS into tmp file to be recalled later?


Running a script which finds a hand full of files and set their directories as variables. I use this script to prompt an osascript dialog box for user input which then calls the file that is similair to the user input.

Everything works. Only issue, when I run the script, it seems to take 10-15 seconds to load dialog box. If I run it again right after the first time, it loads in 2-4 seconds....

Question is:

Is there a way to load the results of my "find" variables faster? Maybe have it store the results in a tmp folder so the next time it runs, it can search just the txt file and put the file directories from there into the variables... (unless they've moved, hence, then I need to do a If-Else statements?)

#!/bin/sh

## READ LOCAL DIR FOR DIALOG BOX ICON
## CHANGE BACK-SLASH TO COLON (USE FOR VARIABLE IN OSASCRIPT)
dir=$(pwd)
dir=${dir//\//:}

abd="$(find ~/ -name "file.1")" 
cbd="$(find ~/ -name "file.2")"  
csd="$(find ~/ -name "file.3")" 
sad="$(find ~/ -name "file 4")" 


workout="$(osascript -e 'Tell application "System Events" to display dialog "Please enter your workout code:" with icon file "'$dir:appIcon.icns'" with title "Title_Here" buttons {"Cancel", "Ok"} default button 2 default answer ""' -e 'text returned of result' 2>/dev/null)"

if [ "$workout" = "ab" ] 
then 
  open "$abd"

elif [ "$workout" = "cb" ] 
then
  open "$cbd"

elif [ "$workout" = "cs" ] 
then
  open "$workout"

elif [ "$workout" = "sa" ] 
then
  open "$sad"

else
  exit     

fi

Solution

  • Well, I found my answer and it seems source does the job. For the script below to make sense, it will be complied in an application via Platypus.

    If there is anyway to compress this, please share.

    #!/bin/sh
    
    ## READ LOCAL DIR
    ## CHANGE BACK-SLASH TO COLON (USE FOR VARIABLE IN OSASCRIPT)
    dir=$(pwd)
    dir=${dir//\//:}
    
    ## REPAIR AND RELOCATE FILE FUNCTION
    ## RUN FUNCTION ON FIRST LOAD AND WHEN USER INPUTS [repair]
    ## STORES VARIABLES IN TMP FOLDER TO BE CALLED LATER
    function repaird() {    
        abd="$(find ~/ -name "FILE 1")" 
        cbd="$(find ~/ -name "FILE 2")"
        csd="$(find ~/ -name "FILE 3")"
        sad="$(find ~/ -name "FILE 4")"
        app="$(find ~/ -name "FILE.app")"  
        echo "#!/bin/sh" > /tmp/VAR_LIST.sh    ##CREATE .SH FILE TO STORE VARIABLES
        echo "" >> /tmp/VAR_LIST.sh
        echo "abd='$abd'" >> /tmp/VAR_LIST.sh
        echo "cbd='$cbd'" >> /tmp/VAR_LIST.sh
        echo "csd='$csd'" >> /tmp/VAR_LIST.sh
        echo "sad='$sad'" >> /tmp/VAR_LIST.sh
        cd /tmp
        chmod +x VAR_LIST.sh; }                
    
    ## CHECK IF VAR_LIST FILE EXIST
    ## IF NOT THEN PROCEED TO FIND FILES AND SAVE VARIABLES TO VAR_LIST
    
    if ls /tmp/VAR_LIST.sh* 1> /dev/null 2>&1; then
        source /tmp/VAR_LIST.sh         ## USE SOURCE TO CALL OUR VAR_LIST 
    else
        repaird
    fi
    
    ## USE OSASCRIPT TO CALL DIALOG BOX WITH USER INPUT    
    
    WORKOUT="$(osascript -e 'Tell application "System Events" to display dialog "Please enter your workout code:" & "\n[ab] = Ab" & "\n[cb] = Cb" & "\n[cs] = Cs" & "\n[sa] = Sa" & "\n[repair] = Repair Directory" with icon file "'$dir:appIcon.icns'" with title "MY_TITLE" buttons {"Cancel", "Ok"} default button 2 default answer ""' -e 'text returned of result' 2>/dev/null)"
    
    ## SEE IF USERS INPUT MATCHES ANY OF THE OPTIONS BELOW
    ## IF NOT THEN EXIT SCRIPT
    
    if [ "$WORKOUT" = "ab" ] 
    then 
      open "$abd"; afplay ohyeah.mp3
    
    elif [ "$WORKOUT" = "cb" ] 
    then
      open "$cbd"; afplay ohyeah.mp3
    
    elif [ "$WORKOUT" = "cs" ] 
    then
      open "$csd"; afplay ohyeah.mp3
    
    elif [ "$WORKOUT" = "sa" ] 
    then
      open "$sad"; afplay ohyeah.mp3
    
    elif [ "$WORKOUT" = "repair" ]       ## REPAIR WILL DO THE FOLLOWING
    then
      rm /tmp/VAR_LIST.sh             ## RM CURRENT VAR_LIST
      repaird                        ## RUN FUNCTION <REPAIRD>
      sleep 5; open "$app" &         ## DELAY 5 SECONDS THEN OPEN SCRIPT AS BACKGROUND
      close "$app"                   ## CLOSE SCRIPT  
    else                             ## SCRIPT OPENS AFTER 5 SECONDS WHICH PROMPTS
      exit                           ## USER WITH SAME 
    
    fi