Skip to content
This repository has been archived by the owner on Feb 21, 2025. It is now read-only.

Commit

Permalink
refactor: unify local home directory query logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Nov 30, 2021
1 parent 5d6d265 commit bef9b64
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
24 changes: 12 additions & 12 deletions LESmain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ end -- if the console is up, close the console. This workaround prevents hammers
-- and the actual redirect can be found inside your hammerspoon folder or at /Contents/Resources/init.lua

function testfirstrun() -- tests if "firstrun.txt" exists. I use this text file on both mac and windows to keep track of the current version.
local filepath = HomePath .. "/.les/resources/firstrun.txt"
local filepath = GetDataPath("resources/firstrun.txt")
local f = io.open(filepath, "r")
if f ~= nil then
io.close(f)
Expand All @@ -38,7 +38,7 @@ if testfirstrun() == false then -- stuff to do when you start the program for th
print("This is the first time running LES")

function setautoadd(newval) -- declaring the function that replaces the "addtostartup" variable in the settings text file to match the users' dialog box selection.
local hFile = io.open(HomePath .. "/.les/settings.ini", "r") -- Reading settings.
local hFile = io.open(GetDataPath("settings.ini"), "r") -- Reading settings.
local restOfFile
local lineCt = 1
local newline = "addtostartup = " .. newval .. [[]]
Expand All @@ -56,7 +56,7 @@ if testfirstrun() == false then -- stuff to do when you start the program for th
end
hFile:close()

hFile = io.open(HomePath .. "/.les/settings.ini", "w") -- write the file.
hFile = io.open(GetDataPath("settings.ini"), "w") -- write the file.
for i, line in ipairs(lines) do
hFile:write(line, "\n")
end
Expand Down Expand Up @@ -108,7 +108,7 @@ end
function testcurrentversion(ver)

print("testing for: " .. ver)
local filepath = (HomePath .. "/.les/resources/version.txt")
local filepath = GetDataPath("resources/version.txt")
local boi = io.open(filepath, "r") -- some of my variable names are super dumb; "version" was already in use so "boi" seemed like the next best choice?

if boi ~= nil then
Expand Down Expand Up @@ -151,12 +151,12 @@ end
-- Integrity checks --
------------------------

-- these functions check the if the files nescesary for the script to function; exist.
-- these functions check the if the files nescesary for the script to function; exist.
-- hammerspoon completely spaces out of they don't.
-- I declare them up here because it fits the theme of this section of the script.

function testsettings()
local filepath = HomePath .. "/.les/settings.ini"
local filepath = GetDataPath("settings.ini")
local f = io.open(filepath, "r")
local var = nil
if f ~= nil then
Expand All @@ -183,7 +183,7 @@ function testsettings()
end

function testmenuconfig()
local filepath = HomePath .. "/.les/menuconfig.ini"
local filepath = GetDataPath("menuconfig.ini")
local f = io.open(filepath, "r")
local var = nil
if f ~= nil then
Expand Down Expand Up @@ -337,7 +337,7 @@ menubartabledebugon = {{
end
}}

filepath = HomePath .. "/.les/resources/strict.txt"
filepath = GetDataPath("resources/strict.txt")
f = io.open(filepath, "r")
if f ~= nil then
io.close(f)
Expand All @@ -355,7 +355,7 @@ filepath = nil -- sets the strict time setting
-- this is what happens when you hit "readme" in the default plugin menu.

function readme()
local readmejingleobj = hs.sound.getByFile(HomePath .. "/.les/resources/readmejingle.wav")
local readmejingleobj = hs.sound.getByFile(GetDataPath("resources/readmejingle.wav"))
readmejingleobj:device(nil)
readmejingleobj:loopSound(false)
readmejingleobj:play()
Expand Down Expand Up @@ -2135,7 +2135,7 @@ function cheatmenu()
ShellCopy(JoinPaths(BundleResourceAssetsPath, "als.als"), ScriptUserResourcesPath)
print("done cloning project")
hs.osascript.applescript([[delay 2
tell application "Finder" to open POSIX file "]] .. HomePath .. [[/.les/resources/als.als"]])
tell application "Finder" to open POSIX file "]] .. GetDataPath([[resources/als.als"]]))
return true
end

Expand Down Expand Up @@ -2254,7 +2254,7 @@ function coolfunc(hswindow, appname, straw) -- function that handles saving and
oldtrackname = trackname
print(_G["timer_" .. oldtrackname])
os.execute([[mkdir ~/.les/resources/time]])
local filepath = HomePath .. [[/.les/resources/time/]] .. oldtrackname .. "_time" .. [[.txt]]
local filepath = GetDataPath([[resources/time/]] .. oldtrackname .. "_time" .. [[.txt]])
local f2 = io.open(filepath, "r")
if f2 ~= nil then
io.close(f2)
Expand All @@ -2281,7 +2281,7 @@ function coolfunc(hswindow, appname, straw) -- function that handles saving and
return
end

filepath = HomePath .. [[/.les/resources/time/]] .. trackname .. "_time" .. [[.txt]] -- loading old time (if it exists)
filepath = GetDataPath([[resources/time/]] .. trackname .. "_time" .. [[.txt]]) -- loading old time (if it exists)
local f = io.open(filepath, "r")
if f ~= nil then
print("timer file found")
Expand Down
4 changes: 4 additions & 0 deletions globals/filepaths.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ BundleResourcePath = BundleContentPath .. "/extensions/hs/les"
BundleResourceAssetsPath = BundleResourcePath .. "/assets"
BundleIconPath = QuoteString(JoinPaths(BundleContentPath, AppIcon))

function GetDataPath(string)
return JoinPaths(ScriptUserPath, string)
end

function GetUserPath(string)
return JoinPaths(ScriptUserResourcesPath, string)
end
Expand Down

0 comments on commit bef9b64

Please sign in to comment.